Quantcast
Channel: "implements Runnable" vs "extends Thread" in Java - Stack Overflow
Viewing all articles
Browse latest Browse all 45

"implements Runnable" vs "extends Thread" in Java

0
0

From what time I've spent with threads in Java, I've found these two ways to write threads:

With implements Runnable:

public class MyRunnable implements Runnable {
    public void run() {
        //Code
    }
}
//Started with a "new Thread(new MyRunnable()).start()" call

Or, with extends Thread:

public class MyThread extends Thread {
    public MyThread() {
        super("MyThread");
    }
    public void run() {
        //Code
    }
}
//Started with a "new MyThread().start()" call

Is there any significant difference in these two blocks of code ?


Viewing all articles
Browse latest Browse all 45

Latest Images

Trending Articles





Latest Images