multithreading 为什么不能直接调用run()方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4830302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can't we directly call the run() method?
提问by n_g
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?
如果一个线程的start()方法内部调用了run()方法,那我们为什么不在代码中直接调用run()方法呢?这样做会涉及哪些问题?
回答by Rob Kennedy
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 currentthread 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 youdidn't include it when you wrote the run
method.
该start
方法确保代码在新的线程上下文中运行。如果你run
直接调用,那么它就像一个普通的方法调用,它会在当前线程而不是新线程的上下文中运行。该start
方法包含触发新线程的特殊代码;run
显然没有这种能力,因为您在编写run
方法时没有包含它。
回答by Varun
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() :
当我们在一个线程对象上调用 start() 方法时, start() 方法通过为线程创建一个新的调用堆栈来启动一个新的执行线程。 start() 使这个线程开始执行并且 Java 虚拟机调用该线程的 run() 方法。如果我们调用 run() 而不是 start() 会怎样:
Though this is legal, but the run() method goes into the current call stack instead of creating a new call stack.
虽然这是合法的,但 run() 方法进入当前调用堆栈而不是创建新的调用堆栈。
For example if the current method being executed is main then the call stack created are:
例如,如果当前正在执行的方法是 main 则创建的调用堆栈是:
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
新线程的调用堆栈(start() 方法创建了一个新的调用堆栈)调用堆栈 - 主线程
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
run() 方法不会为线程创建新的调用堆栈。run() 方法进入当前调用堆栈
回答by Aaron McIver
Calling run
executes the code synchronously; whereas allowing the JVM to call run
via start
would allow the code to execute asynchronously.
调用run
同步执行代码;而允许 JVM 调用run
viastart
将允许代码异步执行。
Calling run
directly is often times beneficial in a testing situation where threading may want to be avoided.
run
在可能希望避免线程的测试情况下,直接调用通常是有益的。
回答by paxdiablo
Because start()
will do it as a separate thread. If you were to just call run()
, that would be part of yourthread (i.e., a function call).
因为start()
将它作为一个单独的线程来做。如果您只是调用run()
,那将是您的线程的一部分(即函数调用)。
And, given that your thread may be an infinite loop waiting for work, that would be a bad thing.
而且,鉴于您的线程可能是一个等待工作的无限循环,这将是一件坏事。
回答by Gaurank Verma
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.
基本上 run() 只会在当前线程的上下文中执行它的主体(这里是 main )但是, start() 调用 OS 以创建一个新线程。start() 将在新创建的线程的上下文中调用 run() 方法。
回答by Nikhil Arora
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.
虽然run()
直接调用是合法的,但它会破坏多线程的目的。线程通过拥有自己的调用堆栈独立工作,如果我们不使用start()
方法,那么该语句的执行堆栈将是该语句正在运行的当前堆栈(main()
大多数情况下是方法)。这将违背在我们的main()
方法或换句话说主堆栈运行时同时运行作业的目的。
回答by Krutik
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.
从主线程调用 run() 方法,run() 方法进入当前调用堆栈,而不是在新调用堆栈的开头。
Exaple for the problem if you directly call run() method :
直接调用 run() 方法的问题示例:
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
1 2 3 4 5 1 2 3 4 5
回答by user1923551
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).
直接调用 run 方法将在主线程中运行该代码。那么就像您的程序将只有一个线程(即操作系统提供的主线程)。
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.
如果您调用 start 方法,它将调用驱动程序层线程管理器为您创建一个线程,然后您的 run 函数将被调用。因此,您的 run 方法将在单独的线程中执行。不在主线程中。