Why do we call the thread object\'s start()
method which in turns calls run()
method, why not we directly call run()
method?
If you directly call run() method its body is executed in context of current thread. When you invoke start()
method a new thread is created and run()
method is executed in this new thread.
I presume you are talking about starting a thread. If that's the case the reason you don't invoke the run method directly is that you then would be calling the method, and not starting the thread.
Even if programmatically we are not creating any thread, For every application, O.S will create a default thread to execute its code with CPU.
Calling run method directly will make that run method execute in that main thread given by O.S.
But the intention of creating a thread class is to make sure that run method executes in a different thread. Unless thread manager of O.S creates a thread, your run method will not get executed in a separate thread. To request O.S to create the separate thread you have to call start() method which will send a request to O.S to create a thread. Once O.S creates a thread, then O.S will automatically call run method of your thread class in that newly created thread context. And hence your purpose of creating a separate thread and executing your run method in a separate thread will be served.
If you call run method directly, then it is like O.S is not creating any thread for you, and default main thread will execute your run method. No point of creating a separate thread class for that!
Hope I am clear. Let me know if you need more explanation to answer your question.
Note: Though books say JVM creates threads, internally JVM will have to send a request to thread manager driver of O.S layer to create a new thread in its thread pool. That's why I use O.S term more here than JVM.
If a thread has been instantiated but not started its is said to be in new state.
Unless until a start() method is invoked on the instance of the thread, it will not said to be alive.
If you do not call a start() method on the newly created thread instance thread is not considered to be alive.
If the start() method is not invoked and the run() method is directly called on the Thread instance, the code inside the run() method will not run in a separate new thread but it will start running in the existing thread.
SEE THE PROBLEM IN EXAMPLE
class Multi extends Thread{
public void run(){
for(int i=1;i<5;i++){
try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
Multi t1=new Multi();
Multi t2=new Multi();
t1.run();
t2.run();
}
}
Output:1
2
3
4
5
1
2
3
4
5
As you can see in the above program that there is no context-switching because here t1 and t2 will be treated as normal object not thread object.
The difference is that if we execute the run method by start()
method, it creates a new thread where we can execute run method otherwise the run()
method will be executed in the thread created by JVM in the public static void main()
method. This is exactly the whole point of multithreading, to run a different action on new threads.
Why do we call the thread object's start() method which in turns calls run() method
No it doesn't. start()
calls the operating system, which starts a new thread, which (to simplify greatly) calls the run()
method. Meanwhile the start()
method has already returned to its caller. They are not equivalent.