If I have a class that implements the Runnable interface, what are the differences between these statements in the main method?
MyThread t1 = new MyThread();
t1.start();
and
MyThread t2 = new MyThread();
new Thread(t2).start();
If I have a class that implements the Runnable interface, what are the differences between these statements in the main method?
MyThread t1 = new MyThread();
t1.start();
and
MyThread t2 = new MyThread();
new Thread(t2).start();
The first call (if it would be a run()) would execute in the current Thread, while the second one would create a new thread that would run the run() method of your Runnable.
in second one you create a new thread that you dont hold in a variable and start this new thread. t2 remains same.
in first one you create t1 and start...
i think, second is meaningless
Whenever your class has to extend another class, use Runnable.
And Runnable interface includes only run method not start.
Since you did t1.start()
, I assume MyThread extends Thread. In this case, the first one is correct.
The second create a new thread, then start the task in yet another thread. One thread is wasted.
To implement a thread in Java you basically have two options. One: you extend the class Thread and implement the method run. That seems to be the case with your example. In that case you should call the start()-method of your Thread.class. That is variant one of the two you provided.
The second variant is if you implement the Runnable-interface. In that case you also implement the method run, but your class doesn't have a start, so you create a new Thread-class with your Runnable as a parameter (as you did in your second example). But that doesn't make sense, if you already extended Thread.
I personally prefer to implement Runnable, because I don't change the behaviour of Thread (that would be extending it), but only use the Thread-functionality.
Assuming that MyThread
only implements the Runnable
interface, the second code snippet is the correct pattern to use. Thread.start()
chains to a native method that will drum up a new thread. It will then call its own run()
, which will call run()
on its instance Runnable
variable. In your second code snippet, this would be an instance of MyThread
, your Runnable
.
The first code snippet, given the information you have provided, will not compile, unless:
start
method on your own. If this is the case, your code will not drum up a new thread and you will only follow the process in the current thread as is defined in your MyThread.start()
.Thread
as well as implementing Runnable
. If this is the case, the Runnable
interface is redundant, as Thread
also implements this interface. Also, you should be careful if you are overriding the start
method, as a call to super is needed if you want to drum up a new thread. For this reason at least, it is good to have a class only implement Runnable
and not exend Thread
. Further, if you are only implementing Runnable
, it would be best not to use the word Thread
in the name.Assuming that
class MyThread extends Thread
Your first call starts your code in a new thread.
The call
new Thread(new MyThread()).start();
will too run your code in MyThread, as the Thread constructor only needs an object implementing the Runnable interface and the run() method. Your MyThread class already has this method so it will be executed by the simple wrapper thread. Note also that your new MyThread remains basically an unstarted thread.
You could try the following small program:
public class MyThread extends Thread {
public void run() {
if (Thread.currentThread() != this) {
System.out.println("Wrapped into a separate thread");
start();
} else {
System.out.println("MyThread run as expected");
}
}
public static void main(String[] args) {
new Thread(new MyThread ()).start();
}
}
To make it less confusing due the names lets have a look at another example:
public class MyJPanel extends JPanel implements Runnable {
public void run() {
System.out.println("Hello MyJPanel");
}
public static void main(String[] args) {
new Thread(new MyJPanel()).start();
}
}