If the start() method of a thread internally calls the run() method, then why don't we directly call the run() method in our code? What are the issues involved in doing so?
The start
method makes sure the code runs in a new thread context. If you called run
directly, then it would be like an ordinary method call and it would run in the context of the current thread instead of the new one. The start
method contains the special code to trigger the new thread; run
obviously doesn't have that ability because you didn't include it when you wrote the run
method.
Thread
class, with its JVM native code that can deal directly with threads and interact with the underlying OS. Since you don't write that special code in run
, it should be obvious that your code won't perform any of those special operations. The start
method makes that happen. It will call run
for you; you just worry about what the thread should do after the JVM starts running it for you. –
Guitarist Calling run
executes the code synchronously; whereas allowing the JVM to call run
via start
would allow the code to execute asynchronously.
Calling run
directly is often times beneficial in a testing situation where threading may want to be avoided.
Because start()
will do it as a separate thread. If you were to just call run()
, that would be part of your thread (i.e., a function call).
And, given that your thread may be an infinite loop waiting for work, that would be a bad thing.
class A implements Runnable
{
public void run()
{
for( int i=0; i<5; i++)
{
System.out.println("Thread-A " +i + " Thread Name: " +Thread.currentThread().getName());
}
}
}
class B implements Runnable
{
public void run()
{
for( int i=0; i<5; i++)
{
System.out.println("Thread-B " +i + " Thread Name: " +Thread.currentThread().getName() );
}
}
}
class MyThread
{
public static void main(String [] args)
{
Thread t1 = new Thread(new A());
Thread t2 = new Thread(new B());
t1.run();
t2.run();
System.out.println("**********************************************************");
t1.start();
t2.start();
}
}
Copy & Paste above code ...then run it,,,and see the difference in output..
Basically run() will just exceute its body in context of current thread (which is main here) But, start() make a call to OS to create a new thread. start() will invoke run() method in the context of newly created thread.
Calling run method directly will run that code in the main thread. Then it is like your program will be having only one thread (i.e main thread given by O.S).
If you call start method, which will call driver layer thread manager to create a thread for you, and from there your run function will be called. And hence your run method will be executed in a separate thread. Not in main thread.
The idea behind the thread is to create new stack everytime new thread starts running.
Invoking the run() method from main thread, the run() method goes onto the current call stack rather than at the beginning of a new call stack.
Exaple for the problem if you directly call run() method :
class TestCallRun2 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.print(i+" ");
}
}
public static void main(String args[]){
TestCallRun2 t1=new TestCallRun2();
TestCallRun2 t2=new TestCallRun2();
t1.run();
t2.run();
}
}
Output:
1 2 3 4 5 1 2 3 4 5
Although calling run()
directly is legal but it will defeat the purpose of multi threading. A thread works independently by having its own calling stack, if we do not use start()
method than the executing stack for that statement would be the current stack through which that statement is running (main()
method in most of the cases). This will defeat the purpose of running a job simultaneously while our main()
method or in other words primary stack is running.
© 2022 - 2024 — McMap. All rights reserved.
start()
andrun()
, like in this answer: #263316 – Florrieflorry