java thread start run 的区别和联系
2014-12-27 21:11
169 查看
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.
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 thread与runnable联系区别
- java基础——Thread和Runnable的区别和联系
- Java中两种多线程实现方法:Thread类和Runnable接口的联系与区别
- Java中Runnable 和 Thread的区别及联系
- Java学习札记6:Thread 和 Runnable 的联系和区别(2)
- Java 多线程 -Thread 和Runnable 的区别和联系
- Java学习札记5:Thread 和 Runnable 的联系和区别(1)
- java中thread和runnable的区别和联系
- Java中 Thread和Runnable实现多线程的区别和联系
- java、c、c++、vc、vc++、vb的区别和联系
- java多线程扩展Thread类和实现runnable接口的联系
- Java基本概念:集合类 List/Set/Map 的区别和联系
- Java中集合类的区别和联系
- java中thread的start()和run()的区别
- java 对象类型Boolean与基本类型boolean的联系、区别
- java中Thread线程中的 start()和run()方法的区别
- java.util.Date, java.util.Calendar, java.sql.Date, java.sql.Time, java.sql.Timestamp区别和联系
- java.util中ArrayList、LinkedList以及Vector它们三者的区别与联系
- java 集合类Array、List、Map区别和联系
- Java重载、覆盖、多态、隐藏的区别与联系