java thread start run 的区别和联系如下,摘取一段外文网站论坛上的解释,它讲的不错。一句话,run()是顺序执行而start()则是并行执行。
Why do we need start() method in Thread class? In Java API description for Thread class is written : "Java Virtual Machine calls the run method of this thread..".
Couldn't we call method run() ourselves, without doing double call: first we call start() method which calls run() method? What is a meaning to do things such complicate?
There is some very small but important difference between using start() and run() methods. Look at two examples below:
Example one:
Code:
Thread one = new Thread();
Thread two = new Thread();
one.run();
two.run();
Example two:
Code:
Thread one = new Thread();
Thread two = new Thread();
one.start();
two.start();
The result of running examples will be different.
In Example one the threads will run sequentially: first, thread number one runs, when it exits the thread number two starts.
In Example two both threads start and run simultaneously.
Conclusion: the start() method call run() method asynchronously (does not wait for any result, just fire up an action), while we run run() method synchronously - we wait when it quits and only then we can run the next line of our code.
本文详细阐述了Java线程类中start()与run()方法的功能区别,通过两个示例代码展示两者在并发执行时的不同表现,强调了start()方法的异步执行特性。
2128

被折叠的 条评论
为什么被折叠?



