三種線程創(chuàng)建方式
- 繼承Thread類,子類重寫run()方法,調用子類的strat()啟動線程。
- 實現(xiàn)Runnable接口,實現(xiàn)run()方法,調用對象start()啟動線程。
- 實現(xiàn)Callable接口,實現(xiàn)call()方法,用FutureTask()封裝實現(xiàn)類。使用FutureTask對象作為Thread對象調用start()啟動線程,調用FutureTask對象的get()方法獲取返回值()。
三種方式的優(yōu)缺點
采用繼承Thread類方式:
- 優(yōu)點:編寫簡單。
- 缺點:因為線程類已經繼承了Thread類,所以不能再繼承其他的父類。
采用實現(xiàn)Runnable接口方式:
- 優(yōu)點:線程類只是實現(xiàn)了Runable接口,還可以繼承其他的類。
- 缺點:編程稍微復雜,如果需要訪問當前線程,必須使用Thread.currentThread()方法。
Runnable和Callable的區(qū)別:
- Callable規(guī)定的方法是call(),Runnable規(guī)定的方法是run()。
- Callable的任務執(zhí)行后可返回值,而Runnable的任務是不能返回值得。
- Call方法可以拋出異常,run方法不可以,因為run方法本身沒有拋出異常,所以自定義的線程類在重寫run的時候也無法拋出異常
- 運行Callable任務可以拿到一個Future對象,表示異步計算的結果。它提供了檢查計算是否完成的方法,以等待計算的完成,并檢索計算的結果。通過Future對象可以了解任務執(zhí)行情況,可取消任務的執(zhí)行,還可獲取執(zhí)行結果。
總結:Runnable和Callable功能一樣的,都是構造線程執(zhí)行的任務;其區(qū)別可以簡單理解為有無返回值的區(qū)別,通常Callable使用的比較多
繼承Thread類
繼承Thread類的話,必須重寫run方法,在run方法中定義需要執(zhí)行的任務。
class MyThread extends Thread{
private static int num = 0;
public MyThread(){
num++;
}
@Override
public void run() {
System.out.println("主動創(chuàng)建的第"+num+"個線程");
}
}
創(chuàng)建好了自己的線程類之后,就可以創(chuàng)建線程對象了,然后通過start()方法去啟動線程。注意,不是調用run()方法啟動線程,run方法中只是定義需要執(zhí)行的任務,如果調用run方法,即相當于在主線程中執(zhí)行run方法,跟普通的方法調用沒有任何區(qū)別,此時并不會創(chuàng)建一個新的線程來執(zhí)行定義的任務。
public class Test {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
class MyThread extends Thread{
private static int num = 0;
public MyThread(){
num++;
}
@Override
public void run() {
System.out.println("主動創(chuàng)建的第"+num+"個線程");
}
}
在上面代碼中,通過調用start()方法,就會創(chuàng)建一個新的線程了。為了分清start()方法調用和run()方法調用的區(qū)別,請看下面一個例子:
public class Test {
public static void main(String[] args) {
System.out.println("主線程ID:"+Thread.currentThread().getId());
MyThread thread1 = new MyThread("thread1");
thread1.start();
MyThread thread2 = new MyThread("thread2");
thread2.run();
}
}
class MyThread extends Thread{
private String name;
public MyThread(String name){
this.name = name;
}
@Override
public void run() {
System.out.println("name:"+name+" 子線程ID:"+Thread.currentThread().getId());
}
}
運行結果:
主線程ID:1
name:thread2 子線程ID:1
name:thread1 子線程ID:8
從輸出結果可以得出以下結論:
1)thread1和thread2的線程ID不同,thread2和主線程ID相同,說明通過run方法調用并不會創(chuàng)建新的線程,而是在主線程中直接運行run方法,跟普通的方法調用沒有任何區(qū)別;
2)雖然thread1的start方法調用在thread2的run方法前面調用,但是先輸出的是thread2的run方法調用的相關信息,說明新線程創(chuàng)建的過程不會阻塞主線程的后續(xù)執(zhí)行。
2.實現(xiàn)Runnable接口
- 定義runnable接口的實現(xiàn)類,并重寫該接口的run()方法,該run()方法的方法體同樣是該線程的線程執(zhí)行體。?
- 創(chuàng)建 Runnable實現(xiàn)類的實例,并依此實例作為Thread的target來創(chuàng)建Thread對象,該Thread對象才是真正的線程對象。?
- 調用線程對象的start()方法來啟動該線程。
package Thread;
import java.util.concurrent.*;
//測試類
public class TestThread {
public static void main(String[] args) throws Exception {
testImplents();
}
public static void testImplents() throws Exception {
MyThreadImplements myThreadImplements = new MyThreadImplements();
Thread t1 = new Thread(myThreadImplements);
Thread t2 = new Thread(myThreadImplements, "my thread -2");
t1.start();
t2.start();
}
}
//線程類
class MyThreadImplements implements Runnable {
@Override
public void run() {
System.out.println("通過實現(xiàn)Runable,線程號:" + Thread.currentThread().getName());
}
}
3. 使用Callable接口
和Runnable接口不一樣,Callable接口提供了一個call()方法作為線程執(zhí)行體,call()方法比run()方法功能要強大。
創(chuàng)建并啟動有返回值的線程的步驟如下:
- 創(chuàng)建Callable接口的實現(xiàn)類,并實現(xiàn)call()方法,然后創(chuàng)建該實現(xiàn)類的實例(從java8開始可以直接使用Lambda表達式創(chuàng)建Callable對象)。
- 使用FutureTask類來包裝Callable對象,該FutureTask對象封裝了Callable對象的call()方法的返回值
- 使用FutureTask對象作為Thread對象的target創(chuàng)建并啟動線程(因為FutureTask實現(xiàn)了Runnable接口)
- 調用FutureTask對象的get()方法來獲得子線程執(zhí)行結束后的返回值
下面是一個例子:
public class Main {
public static void main(String[] args){
MyThread3 th=new MyThread3();
//使用Lambda表達式創(chuàng)建Callable對象
//使用FutureTask類來包裝Callable對象
FutureTask<Integer> future=new FutureTask<Integer>(
(Callable<Integer>)()->{
return 5;
}
);
new Thread(task,"有返回值的線程").start();//實質上還是以Callable對象來創(chuàng)建并啟動線程
try{
System.out.println("子線程的返回值:"+future.get());//get()方法會阻塞,直到子線程執(zhí)行結束才返回
}catch(Exception e){
ex.printStackTrace();
}
}
}
start()和run()的區(qū)別?文章來源:http://www.zghlxwxcb.cn/news/detail-664525.html
(具體區(qū)別可以看【Java面試題】線程中start方法和run方法的區(qū)別?)文章來源地址http://www.zghlxwxcb.cn/news/detail-664525.html
- start()方法用來,開啟線程,但是線程開啟后并沒有立即執(zhí)行,他需要獲取cpu的執(zhí)行權才可以執(zhí)行
- run()方法是由jvm創(chuàng)建完本地操作系統(tǒng)級線程后回調的方法,不可以手動調用(否則就是普通方法)
到了這里,關于【Java面試題】線程創(chuàng)建的三種方式及區(qū)別?的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!