java 运行和启动线程的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15841301/
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
Difference between running and starting a thread
提问by Java Player
i don't understand the difference between starting and running a thread, i tested the both methods and they outputs the same result, first i used a combination of run() and start on the same thread and they did the same function as follows:
我不明白启动和运行线程之间的区别,我测试了这两种方法,它们输出相同的结果,首先我使用了 run() 的组合并在同一线程上启动,它们执行相同的功能,如下所示:
public class TestRunAndStart implements Runnable {
public void run() {
System.out.println("running");
}
public static void main(String[] args) {
Thread t = new Thread(new TestRunAndStart());
t.run();
t.run();
t.start();
}
}
}
the output is:
输出是:
running
running
running
then i saw the javadoc of the run() method said that:
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns
so i tried to use the run() method without a separate runnable as follows :
然后我看到 run() 方法的 javadoc 说:
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns
所以我尝试使用 run() 方法而不使用单独的 runnable,如下所示:
public class TestRun extends Thread {
public void run(){
System.out.println("Running Normal Thread");
}
public static void main(String[]args){
TestRun TR=new TestRun();
TR.run();
}
}
and it also executes the run() method and prints Running Normal Thread
although it's constructed without a separate runnable! so what's the main difference between the two methods
它还执行 run() 方法并打印,Running Normal Thread
尽管它是在没有单独的 runnable 的情况下构建的!那么这两种方法的主要区别是什么
回答by Extreme Coders
Thread.run()
does not spawn a new thread whereas Thread.start()
does, i.e Thread.run
actually runs on the same thread as that of the caller whereas Thread.start()
creates a new thread on which the task is run.
Thread.run()
不会产生新线程,而Thread.start()
会产生新线程,即Thread.run
实际上运行在与调用者相同的线程上,而Thread.start()
创建一个运行任务的新线程。
回答by Denys Séguret
When you call run
, you're just executing the run
method in the current thread. You code thus won't be multi-threaded.
当您调用 时run
,您只是run
在当前线程中执行该方法。因此,您的代码不会是多线程的。
If you call start
, this will start a new thread and the run
method will be executed on this new thread.
如果调用start
,这将启动一个新线程,并且该run
方法将在这个新线程上执行。
回答by Ankur Shanbhag
A thread goes through various stages during its life cycle. When you call start() method thread goes in the runnable state and not the running state.
线程在其生命周期中会经历不同的阶段。当您调用start() 方法时,线程进入可运行状态而不是运行状态。
Then the scheduler pics up one thread from pool of thread waiting to get executed and puts it in the running stage.At this actual execution of the thread takes place.
然后调度程序从等待执行的线程池中提取一个线程并将其置于运行阶段。在这个线程的实际执行发生。
回答by Jamie
In your first example, you are calling the run() method directly from the main thread, and then once from a separate thread by calling t.start().
在您的第一个示例中,您直接从主线程调用 run() 方法,然后通过调用 t.start() 从单独的线程调用一次。
In the second case, t.start() creates a separate thread context, and from that context, calls t.run().
在第二种情况下,t.start() 创建一个单独的线程上下文,并从该上下文调用 t.run()。
You also included the docs for the run() method:
您还包含了 run() 方法的文档:
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns
如果该线程是使用单独的 Runnable 运行对象构造的,则调用该 Runnable 对象的 run 方法;否则,此方法不执行任何操作并返回
This is true, in that if you don't override the run() method, then it will simply return, causing that thread to terminate. You have overridden the run() method in your Thread objects, so the overridden method (which does nothing) is not called.
这是真的,因为如果您不覆盖 run() 方法,那么它只会返回,导致该线程终止。您已经覆盖了 Thread 对象中的 run() 方法,因此不会调用覆盖的方法(不执行任何操作)。