Thread的run()与start()的区别
生活随笔
收集整理的這篇文章主要介紹了
Thread的run()与start()的区别
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Java中thread的start()和run()的區(qū)別:
1.start()方法來(lái)啟動(dòng)線程,真正實(shí)現(xiàn)了多線程運(yùn)行,這時(shí)無(wú)需等待run方法體代碼執(zhí)行完畢而直接繼續(xù)執(zhí)行下面的代碼:
通過(guò)調(diào)用Thread類的start()方法來(lái)啟動(dòng)一個(gè)線程,
這時(shí)此線程是處于就緒狀態(tài),
并沒(méi)有運(yùn)行。
然后通過(guò)此Thread類調(diào)用方法run()來(lái)完成其運(yùn)行操作的,
這里方法run()稱為線程體,
它包含了要執(zhí)行的這個(gè)線程的內(nèi)容,
Run方法運(yùn)行結(jié)束,
此線程終止,
而CPU再運(yùn)行其它線程,
?
2.run()方法當(dāng)作普通方法的方式調(diào)用,程序還是要順序執(zhí)行,還是要等待run方法體執(zhí)行完畢后才可繼續(xù)執(zhí)行下面的代碼:
而如果直接用Run方法,
這只是調(diào)用一個(gè)方法而已,
程序中依然只有主線程--這一個(gè)線程,
其程序執(zhí)行路徑還是只有一條,
這樣就沒(méi)有達(dá)到寫(xiě)線程的目的。
?
舉例說(shuō)明一下:
記住:線程就是為了更好地利用CPU,
提高程序運(yùn)行速率的!
public class TestThread1{
public static void main(String[] args){
Runner1 r=new Runner1();
//r.run();//這是方法調(diào)用,而不是開(kāi)啟一個(gè)線程
Thread t=new Thread(r);//調(diào)用了Thread(Runnable target)方法。且父類對(duì)象變量指向子類對(duì)象。
t.start();
for(int i=0;i<100;i++){
System.out.println("進(jìn)入Main Thread運(yùn)行狀態(tài)");
System.out.println(i);
}
}
}
class Runner1 implements Runnable{ //實(shí)現(xiàn)了這個(gè)接口,jdk就知道這個(gè)類是一個(gè)線程
public void run(){
for(int i=0;i<100;i++){
System.out.println("進(jìn)入Runner1運(yùn)行狀態(tài)");
System.out.println(i);
}
}
}
同時(shí)摘取一段外文網(wǎng)站論壇上的解釋:
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.
1.start()方法來(lái)啟動(dòng)線程,真正實(shí)現(xiàn)了多線程運(yùn)行,這時(shí)無(wú)需等待run方法體代碼執(zhí)行完畢而直接繼續(xù)執(zhí)行下面的代碼:
通過(guò)調(diào)用Thread類的start()方法來(lái)啟動(dòng)一個(gè)線程,
這時(shí)此線程是處于就緒狀態(tài),
并沒(méi)有運(yùn)行。
然后通過(guò)此Thread類調(diào)用方法run()來(lái)完成其運(yùn)行操作的,
這里方法run()稱為線程體,
它包含了要執(zhí)行的這個(gè)線程的內(nèi)容,
Run方法運(yùn)行結(jié)束,
此線程終止,
而CPU再運(yùn)行其它線程,
?
2.run()方法當(dāng)作普通方法的方式調(diào)用,程序還是要順序執(zhí)行,還是要等待run方法體執(zhí)行完畢后才可繼續(xù)執(zhí)行下面的代碼:
而如果直接用Run方法,
這只是調(diào)用一個(gè)方法而已,
程序中依然只有主線程--這一個(gè)線程,
其程序執(zhí)行路徑還是只有一條,
這樣就沒(méi)有達(dá)到寫(xiě)線程的目的。
?
舉例說(shuō)明一下:
記住:線程就是為了更好地利用CPU,
提高程序運(yùn)行速率的!
public class TestThread1{
public static void main(String[] args){
Runner1 r=new Runner1();
//r.run();//這是方法調(diào)用,而不是開(kāi)啟一個(gè)線程
Thread t=new Thread(r);//調(diào)用了Thread(Runnable target)方法。且父類對(duì)象變量指向子類對(duì)象。
t.start();
for(int i=0;i<100;i++){
System.out.println("進(jìn)入Main Thread運(yùn)行狀態(tài)");
System.out.println(i);
}
}
}
class Runner1 implements Runnable{ //實(shí)現(xiàn)了這個(gè)接口,jdk就知道這個(gè)類是一個(gè)線程
public void run(){
for(int i=0;i<100;i++){
System.out.println("進(jìn)入Runner1運(yùn)行狀態(tài)");
System.out.println(i);
}
}
}
同時(shí)摘取一段外文網(wǎng)站論壇上的解釋:
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.
總結(jié)
以上是生活随笔為你收集整理的Thread的run()与start()的区别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 使用Eclipse开发Java Web过
- 下一篇: J2EE项目代码编写规范分享