有關Java5線程新特徵的內容全部在java.util.concurrent下面,裏面包含數目衆多的接口和類,熟悉這部分API特徵是一項艱難的學習過程。目前有關這方面的資料和書籍都少之又少,大所屬介紹線程方面書籍還停留在java5之前的知識層面上。
當然新特徵對做多線程程序沒有必須的關係,在java5之前通用可以寫出很優秀的多線程程序。只是代價不一樣而已。
線程池的基本思想還是一種對象池的思想,開闢一塊內存空間,裏面存放了衆多(未死亡)的線程,池中線程執行調度由池管理器來處理。當有線程任務時,從池中取一個,執行完成後線程對象歸池,這樣可以避免反覆創建線程對象所帶來的性能開銷,節省了系統的資源。
在Java5之前,要實現一個線程池是相當有難度的,現在Java5爲我們做好了一切,我們只需要按照提供的API來使用,即可享受線程池帶來的極大便利。
Java5的線程池分好多種:具體的可以分爲兩類,固定尺寸的線程池、可變尺寸連接池。
在使用線程池之前,必須知道如何去創建一個線程池,在Java5中,需要了解的是java.util.concurrent.Executors類的API,這個類提供大量創建連接池的靜態方法,是必須掌握的。
一、固定大小的線程池,newFixedThreadPool:
- package app.executors;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ExecutorService;
- /**
- * Java線程:線程池
- *
- * @author 馮小衛
- */
- public class Test {
- public static void main(String[] args) {
- // 創建一個可重用固定線程數的線程池
- ExecutorService pool = Executors.newFixedThreadPool(5);
- // 創建線程
- Thread t1 = new MyThread();
- Thread t2 = new MyThread();
- Thread t3 = new MyThread();
- Thread t4 = new MyThread();
- Thread t5 = new MyThread();
- // 將線程放入池中進行執行
- pool.execute(t1);
- pool.execute(t2);
- pool.execute(t3);
- pool.execute(t4);
- pool.execute(t5);
- // 關閉線程池
- pool.shutdown();
- }
- }
- class MyThread extends Thread {
- @Override
- public void run() {
- System.out.println(Thread.currentThread().getName() + "正在執行。。。");
- }
- }
輸出結果:
- pool-1-thread-1正在執行。。。
- pool-1-thread-3正在執行。。。
- pool-1-thread-4正在執行。。。
- pool-1-thread-2正在執行。。。
- pool-1-thread-5正在執行。。。
改變ExecutorService pool = Executors.newFixedThreadPool(5)中的參數:ExecutorService pool = Executors.newFixedThreadPool(2),輸出結果是:
- pool-1-thread-1正在執行。。。
- pool-1-thread-1正在執行。。。
- pool-1-thread-2正在執行。。。
- pool-1-thread-1正在執行。。。
- pool-1-thread-2正在執行。。。
從以上結果可以看出,newFixedThreadPool的參數指定了可以運行的線程的最大數目,超過這個數目的線程加進去以後,不會運行。其次,加入線程池的線程屬於託管狀態,線程的運行不受加入順序的影響。
二、單任務線程池,newSingleThreadExecutor:
僅僅是把上述代碼中的ExecutorService pool = Executors.newFixedThreadPool(2)改爲ExecutorService pool = Executors.newSingleThreadExecutor();
輸出結果:
- pool-1-thread-1正在執行。。。
- pool-1-thread-1正在執行。。。
- pool-1-thread-1正在執行。。。
- pool-1-thread-1正在執行。。。
- pool-1-thread-1正在執行。。。
可以看出,每次調用execute方法,其實最後都是調用了thread-1的run方法。
三、可變尺寸的線程池,newCachedThreadPool:
與上面的類似,只是改動下pool的創建方式:ExecutorService pool = Executors.newCachedThreadPool();
輸出:
- pool-1-thread-1正在執行。。。
- pool-1-thread-2正在執行。。。
- pool-1-thread-4正在執行。。。
- pool-1-thread-3正在執行。。。
- pool-1-thread-5正在執行。。。
這種方式的特點是:可根據需要創建新線程的線程池,但是在以前構造的線程可用時將重用它們。
四、延遲連接池,newScheduledThreadPool:
- package app.executors;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.TimeUnit;
- /**
- * Java線程:線程池
- *
- * @author 馮小衛
- */
- public class Test {
- public static void main(String[] args) {
- // 創建一個線程池,它可安排在給定延遲後運行命令或者定期地執行。
- ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
- // 創建實現了Runnable接口對象,Thread對象當然也實現了Runnable接口
- Thread t1 = new MyThread();
- Thread t2 = new MyThread();
- Thread t3 = new MyThread();
- // 將線程放入池中進行執行
- pool.execute(t1);
- // 使用延遲執行風格的方法
- pool.schedule(t2, 1000, TimeUnit.MILLISECONDS);
- pool.schedule(t3, 10, TimeUnit.MILLISECONDS);
- // 關閉線程池
- pool.shutdown();
- }
- }
- class MyThread extends Thread {
- @Override
- public void run() {
- System.out.println(Thread.currentThread().getName() + "正在執行。。。");
- }
- }
讀者可以嘗試改變Executors.newScheduledThreadPool(2)的參數來得到更多的體驗,當然,讓
- @Override
- public void run() {
- System.out.println(Thread.currentThread().getName() + "正在執行。。。");
- }
變成一個無限循環,你可以得到更多的關於pool.shutdown()的用法。
五:單任務延遲連接池(和上面類似,就不寫了)。當然我們也可以自定義線程池,這裏就不寫了,累啊……