線程的創建大多使用Thread類或者Runnable,其實還有第三種Callable。
相比之下Thread類比較簡單但使用限制比較大。
Runnable應用靈活也是最常用的一種方式。
Callable應用較少,但可以及時返回線程處理的結果。
一、繼承Thread類創建線程類
(1)定義Thread類的子類,并重寫該類的run方法,該run方法的方法體就代表了線程要完成的任務。因此把run()方法稱為執行體。
(2)創建Thread子類的實例,即創建了線程對象。
(3)調用線程對象的start()方法來啟動該線程。
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() + "運行 : " + i );
}
}
}
二、通過Runnable接口創建線程類
(1)定義runnable接口的實現類,并重寫該接口的run()方法,該run()方法的方法體同樣是該線程的線程執行體。
(2)創建 Runnable實現類的實例,并依此實例作為Thread的target來創建Thread對象,該Thread對象才是真正的線程對象。
(3)調用線程對象的start()方法來啟動該線程。
事實上,當傳入一個Runnable target參數給Thread后,Thread的run()方法就會調用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() + "運行 : " + i );
}
}
}
三、通過Callable和Future創建線程
(1)創建Callable接口的實現類,并實現call()方法,該call()方法將作為線程執行體,并且有返回值。
(2)創建實現了Callable接口的實現類的實例,使用FutureTask類來包裝Callable對象,該FutureTask對象封裝了該Callable對象的call()方法的返回值。
(3)使用FutureTask對象作為Thread對象的target創建并啟動新線程。
(4)調用FutureTask對象的get()方法來獲得子線程執行結束后的返回值
實例代碼:
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.執行Callable方式,需要FutureTask實現類的支持,用于接收運算結果
FutureTask<Integer> result = new FutureTask<>(td);
new Thread(result).start();
// 2.接收線程運算后的結果
Integer sum;
try {
//等所有線程執行完,獲取值,因此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;
}
}
結果
0
1
2
3
4
5
6
7
8
9
10
-----------------------------
55
實際開發中應該在具體的情況下分析使用哪一種開始線程的方式。