java中run()和start()的区别
2011-11-28 10:12
344 查看
class NewThread implements Runnable {
Thread t;
public NewThread() {
t = new Thread(this,"Demo thread");
System.out.println("Child thread : " + t);
t.run();
}
public void run(){
try{
for( int i = 5; i > 0; i --){
System.out.println("Child thread :" + i);
Thread.sleep(500);
}
}catch(InterruptedException e){
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
public class TestDemo{
public static void main(String args[]){
new NewThread();
try{
for( int i = 5; i > 0; i --){
System.out.println("Main thread :" + i);
Thread.sleep(1000);
}
}catch(InterruptedException e){
System.out.println("Main interrupted.");
}
System.out.println("Exiting Main thread.");
}
}
这是一个实现多线程的程序,运行结果如下:
Child thread : Thread[Demo thread,5,main]
Main thread :5
Child thread :5
Child thread :4
Main thread :4
Child thread :3
Child thread :2
Main thread :3
Child thread :1
Exiting child thread.
Main thread :2
Main thread :1
Exiting Main thread.
试想,如果把 start()改成run()会出现什么结果?
修改之后运行结果:
Child thread : Thread[Demo thread,5,main]
Child thread :5
Child thread :4
Child thread :3
Child thread :2
Child thread :1
Exiting child thread.
Main thread :5
Main thread :4
Main thread :3
Main thread :2
Main thread :1
Exiting Main thread.
程序运行成为了单线程顺序执行。为什么?
start方法:用来启动一个线程, 这时此线程是处于就绪状态, 并没有运行。 然后通过此Thread类调用方法run()来完成其运行操作的, 这里方法run()称为线程体, 它包含了要执行的这个线程的内容, run方法运行结束, 此线程终止, 而CPU再运行其它线程,
直接用run方法: 这只是调用一个方法而已, 程序中依然只有主线程--这一个线程, 其程序执行路径还是只有一条, 这样就没有达到写线程的目的。
记住:线程就是为了更好地利用CPU,提高程序运行速率的!
让一个类继承Thread,重写run()方法,然后调用时使用:new ThreadImp().start()方法
来启动该类的实例!不要用new ThreadImp().run() !!!
来自?http://libingye.iteye.com/blog/930615
Thread t;
public NewThread() {
t = new Thread(this,"Demo thread");
System.out.println("Child thread : " + t);
t.run();
}
public void run(){
try{
for( int i = 5; i > 0; i --){
System.out.println("Child thread :" + i);
Thread.sleep(500);
}
}catch(InterruptedException e){
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
public class TestDemo{
public static void main(String args[]){
new NewThread();
try{
for( int i = 5; i > 0; i --){
System.out.println("Main thread :" + i);
Thread.sleep(1000);
}
}catch(InterruptedException e){
System.out.println("Main interrupted.");
}
System.out.println("Exiting Main thread.");
}
}
这是一个实现多线程的程序,运行结果如下:
Child thread : Thread[Demo thread,5,main]
Main thread :5
Child thread :5
Child thread :4
Main thread :4
Child thread :3
Child thread :2
Main thread :3
Child thread :1
Exiting child thread.
Main thread :2
Main thread :1
Exiting Main thread.
试想,如果把 start()改成run()会出现什么结果?
修改之后运行结果:
Child thread : Thread[Demo thread,5,main]
Child thread :5
Child thread :4
Child thread :3
Child thread :2
Child thread :1
Exiting child thread.
Main thread :5
Main thread :4
Main thread :3
Main thread :2
Main thread :1
Exiting Main thread.
程序运行成为了单线程顺序执行。为什么?
start方法:用来启动一个线程, 这时此线程是处于就绪状态, 并没有运行。 然后通过此Thread类调用方法run()来完成其运行操作的, 这里方法run()称为线程体, 它包含了要执行的这个线程的内容, run方法运行结束, 此线程终止, 而CPU再运行其它线程,
直接用run方法: 这只是调用一个方法而已, 程序中依然只有主线程--这一个线程, 其程序执行路径还是只有一条, 这样就没有达到写线程的目的。
记住:线程就是为了更好地利用CPU,提高程序运行速率的!
让一个类继承Thread,重写run()方法,然后调用时使用:new ThreadImp().start()方法
来启动该类的实例!不要用new ThreadImp().run() !!!
来自?http://libingye.iteye.com/blog/930615
相关文章推荐
- Java基础加强之并发(三)Thread中start()和run()的区别
- Java线程中run和start方法的区别
- Java并发编程--Thread中start()与run()方法的区别概要
- java中thread的start()和run()的区别
- Java Thread中start()和run()的区别_动力节点Java学院整理
- java start和run的区别
- Java多线程系列--“基础篇”03之 Thread中start()和run()的区别
- Java多线程-run方法与start方法的区别
- java线程中run和start方法的区别
- Java线程之run和start的区别
- JAVA多线程之——Thread中start()和run()的区别
- Java线程中run和start方法的区别
- java线程中run和start的区别
- java多线程之wait notify详解,start于run区别,wait与sleep区别一篇通,附例:生产者消费者。
- java中thread线程start和run的区别
- Java New Thread start() 与 run()的区别
- java之Thread线程相关yield()、sleep()、wait()、join()、run和start区别详解
- Java多线程系列--“基础篇”03之 Thread中start()和run()的区别
- Java线程中run和start方法的区别
- java 多线程系列基础篇(三)之start()和run()的区别