您的位置:首页 > 编程语言 > Java开发

java中thread的start()和run()的区别

2011-08-08 20:23 363 查看
java中thread的start()和run()的区别:

1.start()方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码:

通过调用Thread类的start()方法来启动一个线程,

这时此线程是处于就绪状态,

并没有运行。

然后通过此Thread类调用方法run()来完成其运行操作的,

这里方法run()称为线程体,

它包含了要执行的这个线程的内容,

Run方法运行结束,

此线程终止,

而CPU再运行其它线程,

2.run()方法当作普通方法的方式调用,程序还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码:

而如果直接用Run方法,

这只是调用一个方法而已,

程序中依然只有主线程--这一个线程,

其程序执行路径还是只有一条,

这样就没有达到写线程的目的。

举例说明一下:

记住:线程就是为了更好地利用CPU,

提高程序运行速率的!

public class TestThread1{

public static void main(String[] args){

Runner1 r=new Runner1();

//r.run();//这是方法调用,而不是开启一个线程

Thread t=new Thread(r);//调用了Thread(Runnable target)方法。且父类对象变量指向子类对象。

t.start();

for(int i=0;i<100;i++){

System.out.println("进入Main Thread运行状态");

System.out.println(i);

}

}

}

class Runner1 implements Runnable{ //实现了这个接口,jdk就知道这个类是一个线程

public void run(){

for(int i=0;i<100;i++){

System.out.println("进入Runner1运行状态");

System.out.println(i);

}

}

}

同时摘取一段外文网站论坛上的解释:

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.

转载地址:/article/9316880.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: