當(dāng)前位置:首頁 > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 講師博文 > Java開發(fā):線程創(chuàng)建的三種方式
線程的創(chuàng)建大多使用Thread類或者Runnable,其實(shí)還有第三種Callable。
相比之下Thread類比較簡單但使用限制比較大。
Runnable應(yīng)用靈活也是最常用的一種方式。
Callable應(yīng)用較少,但可以及時(shí)返回線程處理的結(jié)果。
一、繼承Thread類創(chuàng)建線程類
(1)定義Thread類的子類,并重寫該類的run方法,該run方法的方法體就代表了線程要完成的任務(wù)。因此把run()方法稱為執(zhí)行體。
(2)創(chuàng)建Thread子類的實(shí)例,即創(chuàng)建了線程對象。
(3)調(diào)用線程對象的start()方法來啟動(dòng)該線程。
public class Main {
public static void main(String[] args) throws Exception {
myThread th1 = new myThread();
myThread th2 = new myThread();
th1.start();
th2.start();
}
}
class myThread extends Thread {
public void run() {
for(int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + "運(yùn)行 : " + i );
}
}
}
二、通過Runnable接口創(chuàng)建線程類
(1)定義runnable接口的實(shí)現(xiàn)類,并重寫該接口的run()方法,該run()方法的方法體同樣是該線程的線程執(zhí)行體。
(2)創(chuàng)建 Runnable實(shí)現(xiàn)類的實(shí)例,并依此實(shí)例作為Thread的target來創(chuàng)建Thread對象,該Thread對象才是真正的線程對象。
(3)調(diào)用線程對象的start()方法來啟動(dòng)該線程。
事實(shí)上,當(dāng)傳入一個(gè)Runnable target參數(shù)給Thread后,Thread的run()方法就會(huì)調(diào)用target.run(),參考JDK源代碼:
public void run() {
if (target != null) {
target.run();
}
}
示例代碼為:
public class Main {
public static void main(String[] args) throws Exception {
myThread myth = new myThread();
Thread th1 = new Thread(myth);
Thread th2 = new Thread(myth);
th1.start();
th2.start();
}
}
class myThread implements Runnable {
public void run() {
for(int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + "運(yùn)行 : " + i );
}
}
}
三、通過Callable和Future創(chuàng)建線程
(1)創(chuàng)建Callable接口的實(shí)現(xiàn)類,并實(shí)現(xiàn)call()方法,該call()方法將作為線程執(zhí)行體,并且有返回值。
(2)創(chuàng)建實(shí)現(xiàn)了Callable接口的實(shí)現(xiàn)類的實(shí)例,使用FutureTask類來包裝Callable對象,該FutureTask對象封裝了該Callable對象的call()方法的返回值。
(3)使用FutureTask對象作為Thread對象的target創(chuàng)建并啟動(dòng)新線程。
(4)調(diào)用FutureTask對象的get()方法來獲得子線程執(zhí)行結(jié)束后的返回值
實(shí)例代碼:
package test4;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class Main {
public static void main(String[] args) {
ThreadDemo td = new ThreadDemo();
// 1.執(zhí)行Callable方式,需要FutureTask實(shí)現(xiàn)類的支持,用于接收運(yùn)算結(jié)果
FutureTask<Integer> result = new FutureTask<>(td);
new Thread(result).start();
// 2.接收線程運(yùn)算后的結(jié)果
Integer sum;
try {
//等所有線程執(zhí)行完,獲取值,因此FutureTask 可用于 閉鎖
sum = result.get();
System.out.println("-----------------------------");
System.out.println(sum);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
class ThreadDemo implements Callable<Integer> {
@Override
public Integer call() throws Exception {
int sum = 0;
for (int i = 0; i <= 10; i++) {
System.out.println(i);
sum += i;
}
return sum;
}
}
結(jié)果
0
1
2
3
4
5
6
7
8
9
10
-----------------------------
55
實(shí)際開發(fā)中應(yīng)該在具體的情況下分析使用哪一種開始線程的方式。