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?
问题:
回答1:
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.
回答2:
When we call the start() method on a thread object, the start() method starts a new thread of execution by creating a new call stack for the thread .The start() causes this thread to begin execution and the Java Virtual Machine calls the run() method of this thread. What if we call run() instead of start() :
Though this is legal, but the run() method goes into the current call stack instead of creating a new call stack.
For example if the current method being executed is main then the call stack created are:
class MyThread extends Thread
{
public void run()
{
System.out.println("running");
}
}
public class ThreadDemo
{
public static void main (String[] args )
{
MyThread thread=new MyThread();
thread.start();
}
}
Call stack for new thread (start() method created a new call stack) Call Stack - main Thread
class MyThread extends Thread
{
public void run()
{
System.out.println("running");
}
}
public class ThreadDemo
{
public static void main (String[] args )
{
MyThread thread=new MyThread();
thread.run();
}
}
run() method does not create a new call stack for the thread. run() method goes into the current call stack
回答3:
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.
回答4:
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.
回答5:
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.
回答6:
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.
回答7:
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
回答8:
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.