I stumbled upon a doubt.
I understand the basic difference between start()
and run()
function in Thread
class and Runnable
interface.
In below code snippet.
public class ThreadTest extends Thread{
public void run(){
for(int i = 0; i < 3; i ){
System.out.println("-->" this.getName() ", " i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ThreadTest t1 = new ThreadTest();
t1.setName("firstThread");
ThreadTest t2 = new ThreadTest();
t2.setName("secondThread");
t1.start();
t2.start();
}
}
Output:
-->firstThread, 0
-->secondThread, 0
-->firstThread, 1
-->secondThread, 1
-->firstThread, 2
-->secondThread, 2
If I change t1.start()
and t2.start()
to t1.run()
and t2.run()
. I get the below output:
-->firstThread, 0
-->firstThread, 1
-->firstThread, 2
-->secondThread, 0
-->secondThread, 1
-->secondThread, 2
I have 2 doubts here:
- Why there is difference in output in case of
run()
andstart()
? - It's said that,
start()
method creates a new thread and then runs it, whilerun()
method just runs the thread. Then what isnew
operator doing here? I suppose,new
is creating the thread.
Can someone please clarify.
CodePudding user response:
Even though the given answers mostly are correct, there's something deficit here. So, I thought of adding that missing pieces in my answer. If you directly create a Thread and start it in your main thread, that thread will get executed and main thread immediately continues to the next instruction. On the contrary, if you just call Runnable.run
method directly, then your main thread executes the run
method, waiting for it's completion and moves to the next instruction. That's why you get a different output. However you can do a little bit of tweak in your code and see it for yourself. Here are the changes you need to make.
public void run() {
for (int i = 0; i < 3; i ) {
System.out.println(Thread.currentThread().getName() ", " i);
// Remainder omitted for the sake of brevity.
}
}
What's wrong in your approach is, by just calling this.getName()
returns the name
instance field of your thread object, which is just an illusion but not giving you the exact thread which executes your task. If you need to get that, you have to use Thread.currentThread().getName()
instead. Now here's the output with start
firstThread, 0
secondThread, 0
firstThread, 1
secondThread, 1
firstThread, 2
secondThread, 2
And here's the output with run
method
main, 0
main, 1
main, 2
main, 0
main, 1
main, 2
The main thread executes each and every task sequentially. Also if you read the documentation of the start
method it clearly states,
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).
CodePudding user response:
new
just creates an Object of ThreadTest
and by run()
you are just calling the run method from the main thread. new
on Thread class does not create new thread unless start is called.
According to the documentation, run() executes the current thread and start() creates a new thread and executes run() on the new thread.
So when you call start() it creates 2 separate thread and runs them individually. Hence the output:
-->firstThread, 0
-->firstThread, 1
-->firstThread, 2
-->secondThread, 0
-->secondThread, 1
-->secondThread, 2
Calling run() is simply like calling normal method in the order. Hence the output:
-->firstThread, 0
-->secondThread, 0
-->firstThread, 1
-->secondThread, 1
-->firstThread, 2
-->secondThread, 2
CodePudding user response:
Each Java program running is a process, and the thread that invokes the main
method called main thread
.
run()
method is just like a POJO method which inherited either from Thread
class or overriden from Runnable
interface. Calling run()
method (even from a thread like thread1.run()
) is not creating a new thread, instead, it simply calls run()
method from main
thread.
start()
method creates a new thread, links it to the object it is called with. In this case t1.start()
, there is an object t1
which was just holding a reference address of newly created object.
start()
creates an object for thread and start and then run
it.
Hence, in given example code both instance of start
calling is different as they create two different threads.
While run
method is invoked from main
thread, hence the same.
This is the reason for difference in output.