線程的創建一般有4種形式,即:
1.繼承Thread類並重寫run()方法
2.實現Runnable接口並重寫run()方法
3.實現Callable接口並重寫call()方法
4.通過線程池創建
本文主要回顧前三種創建方法,線程池的使用會單寫一篇
1.繼承Thread類
首先我們定義一個繼承了Thread類的MyThread
class MyThread extends Thread {
private int value = 5;
@Override
public void run(){
System.out.println(currentThread().getName()+" value is "+--value);
}
}
然後我們通過使用實例化MyThread的方式開啓線程
public static void main(String[] args) {
MyThread test0 = new MyThread();//新建MyThread實例
MyThread test1 = new MyThread();//新建MyThread實例
MyThread test2 = new MyThread();//新建MyThread實例
test0.start();//運行線程
test1.start();//運行線程
test2.start();//運行線程
}
我們重寫run()方法之後需要用start()方法開始線程,start()方法通知JVM來調用當前線程的run()方法。要注意的是,一個線程不可以調用兩次start()——It is never legal to start a thread more than once.In particular, a thread may not be restarted once it has completed execution.
輸出結果:
可以看到我們線程的默認名字是Thread-n,三個線程的value都是4,說明他們操作的是各自的實例方法。
下面我們在用public Thread(Runnable target)
的方法開啓線程
public static void main(String[] args) {
MyThread test = new MyThread();//新建MyThread實例
Thread thread0 = new Thread(test);//通過帶參Thread構造函數創建線程,參數test是我們繼承了Thread的MyThread
Thread thread1 = new Thread(test);
Thread thread2 = new Thread(test);
test.start();
thread0.start();//運行線程
thread1.start();
thread2.start();
}
輸出結果:
可以看到我們創建的線程在啓動時,操作的都是同一個實例的run()方法,也就是test實例的方法,所以value的值是共享的。所以有些文章說繼承Thread類和實現Runnable接口這兩種創建線程方式的區別是一個共享另一個不共享,但其實深層原因是調用了不同的Thread構造函數。Thread類也是實現了Runnable接口的類,最終新建線程都要創建一個Thread類。
2.實現Runnable接口
這種實現方式與第一種差別不大,只是我們的類實現了Runnable接口,符合public Thread(Runnable target)
構造函數的參數要求,然後再實例化一個Thread對象新建線程。
我們實現了Runnable接口的類:
class MyThread implements Runnable {
private int value = 5;
@Override
public void run(){
System.out.println(Thread.currentThread().getName()" value is "+--value);
}
}
由於只是實現接口,並沒有繼承Thread類,所以無法直接調用currentThread()方法(這個方法是Thread類的一個本地方法public static native Thread currentThread();
),而是通過Thread.currentThread()
獲取當先線程對象
public static void main(String[] args) {
MyThread test = new MyThread();//新建MyThread實例
Thread thread0 = new Thread(test);//通過帶參Thread構造函數創建線程,參數test是我們繼承了Thread的MyThread
Thread thread1 = new Thread(test);
Thread thread2 = new Thread(test);
thread0.start();//運行線程
thread1.start();
thread2.start();
}
輸出結果:
同樣的因爲操作同一個對象的run()方法,所以屬性是共享的。
我們再讓每個Thread操作不同的Mythread實例
public static void main(String[] args) {
MyThread test = new MyThread();//新建MyThread實例
MyThread test1 = new MyThread();//新建MyThread實例
MyThread test2 = new MyThread();//新建MyThread實例
Thread thread0 = new Thread(test);//通過帶參Thread構造函數創建線程,參數test是我們繼承了Thread的MyThread
Thread thread1 = new Thread(test1);
Thread thread2 = new Thread(test2);
thread0.start();//運行線程
thread1.start();
thread2.start();
}
實現Runnable接口的形式也可以實現線程隔離
3.通過實現Callable接口來實現多線程
通過實現Callable接口創建的線程可以實現獲取返回值,取消等。值得注意的是,FutureTask是一個可取消的異步計算,當計算完成時FutureTask便不再可用。
首先我們需要一個實現了Callable接口的類(重寫其中的**call()**方法)
class MyThread implements Callable<Integer> {
private int value = 5;
@Override
public Integer call() {
System.out.println(Thread.currentThread().getName()+" value is "+--value);
return value;
}
}
然後在通過FutureTask創建線程並啓動
public static void main(String[] args) {
MyThread test = new MyThread();//新建MyThread實例
FutureTask task = new FutureTask(test);//創建一個FutureTask,參數是我們的MyThread實例
Thread thread0 = new Thread(task);//將FutureTask傳入Thread來創建線程
Thread thread1 = new Thread(task);
Thread thread2 = new Thread(task);
thread0.start();//運行線程並打印
thread1.start();
thread2.start();
}
輸出結果
可以看到只有一個線程成功啓動,因爲Thread-0執行結束後,FutureTask也就不可用了,後面的線程自然無法啓動。如果要FutureTask可複用的話需要調用它的protected boolean runAndReset()
方法。
我們在多創建幾個FutureTask實例
public static void main(String[] args) {
MyThread test = new MyThread();//新建MyThread實例
FutureTask task = new FutureTask(test);//創建一個FutureTask,參數是我們的MyThread實例
FutureTask task1 = new FutureTask(test);
FutureTask task2 = new FutureTask(test);
Thread thread0 = new Thread(task);//將FutureTask傳入Thread來創建線程
Thread thread1 = new Thread(task1);
Thread thread2 = new Thread(task2);
thread0.start();//運行線程並打印
thread1.start();
thread2.start();
}
輸出結果爲:
雖然是不同的FutureTask,但是他們調用的都是同一個MyThread實例的call()方法。所以實例屬性也是共享的。
獲取FutureTask的返回值
public static void main(String[] args) throws ExecutionException, InterruptedException {
MyThread test = new MyThread();//新建MyThread實例
FutureTask task = new FutureTask(test);//創建一個FutureTask,參數是我們的MyThread實例
Thread thread0 = new Thread(task);//將FutureTask傳入Thread來創建線程
thread0.start();//運行線程並打印
System.out.println("task運行結果"+task.get());
}
利用FutureTask提供的get()方法可以獲得線程的返回值,輸出結果:
FutureTask也提供了其他的方法,就不一一列舉;
總之FutureTask是RunnableFuture接口的一個實現public class FutureTask<V> implements RunnableFuture<V>
,而RunnableFuture接口繼承了Runnable接口和Future接口public interface RunnableFuture<V> extends Runnable, Future<V>
,所以FutureTask可以作爲參數傳給Thread來實現創建線程的功能,同時實現Future接口又實現了獲取返回值等異步操作。妙啊。