線程池的基本思想還是一種對象池的思想,開辟一塊內存空間,里面存放了眾多(未死亡)的線程,池中線程執行調度由池管理器來處理。當有線程任務時,從池中取一個,執行完成后線程對象歸池,這樣可以避免反復創建線程對象所帶來的性能開銷,節省了系統的資源。
在Java5之前,要實現一個線程池是相當有難度的,現在Java5為我們做好了一切,我們只需要按照提供的API來使用,即可享受線程池帶來的極大便利。
Java5的線程池分好多種:具體的可以分為兩類,固定尺寸的線程池、可變尺寸連接池。
Executor框架主要包含三個部分:
任務:包括Runnable和Callable,其中Runnable表示一個可以異步執行的任務,而Callable表示一個會產生結果的任務
任務的執行:包括Executor框架的核心接口Executor以及其子接口ExecutorService。在Executor框架中有兩個關鍵類ThreadPoolExecutor和ScheduledThreadPoolExecutor實現了ExecutorService接口。
異步計算的結果:包括接口Future和其實現類FutureTask。
Executor接口(java.util.concurrent.Executor)
它是Executor的基礎與核心,其定義如下:
public interface Executor {
void execute(Runnable command);
}
它包含了一個方法execute,參數為一個Runnable接口引用。
Executor接口將任務的提交與執行分離開來。
ThreadPoolExecutor類(java.util.concurrent.ThreadPoolExecutor)
它是線程池的核心實現類,用來執行被提交的任務。
它通常由工廠類Executors來創建,Executors可以創建SingleThreadExecutor,FixedThreadPool以及CachedThreadPool等不同的ThreadPoolExecutor。
一、固定大小的線程池,newFixedThreadPool:
滿足了資源管理的需求,可以限制當前線程數量。適用于負載較重的服務器環境。
packageapp.executors;
importjava.util.concurrent.Executors;
importjava.util.concurrent.ExecutorService;
/**
*?Java線程:線程池*
*?@author?馮小衛
*/
publicclassTest?{
publicstaticvoidmain(String[]?args)?{
//?創建一個可重用固定線程數的線程池
ExecutorService?pool?=?Executors.newFixedThreadPool(5);
//?創建線程
Thread?t1?=newMyThread();
Thread?t2?=newMyThread();
Thread?t3?=newMyThread();
Thread?t4?=newMyThread();
Thread?t5?=newMyThread();
//?將線程放入池中進行執行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
//?關閉線程池
pool.shutdown();
}
}
classMyThreadextendsThread?{
@Override
publicvoidrun()?{
System.out.println(Thread.currentThread().getName()?+"正在執行。。。");
}
}
輸出結果:
[html]view plaincopy
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),輸出結果是:
[html]view plaincopy
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();
SingleThreadExecutor保證了任務執行的順序,不會存在多線程活動。
輸出結果:
[html]view plaincopy
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();
適用于執行很多短期異步任務的小程序,適用于負載較輕的服務器
輸出:
[html]view plaincopy
pool-1-thread-1正在執行。。。
pool-1-thread-2正在執行。。。
pool-1-thread-4正在執行。。。
pool-1-thread-3正在執行。。。
pool-1-thread-5正在執行。。。
這種方式的特點是:可根據需要創建新線程的線程池,但是在以前構造的線程可用時將重用它們。
四、延遲連接池,newScheduledThreadPool:
它可以在給定的延遲時間后執行命令,或者定期執行命令,它比Timer更強大更靈活。
ScheduledThreadPoolExecutor具有固定線程個數,適用于需要多個后臺線程執行周期任務,并且為了滿足資源管理需求而限制后臺線程數量的場景
它適用于單個后臺線程執行周期任務,并且保證順序一致執行的場景。
[java]view plaincopy
packageapp.executors;
importjava.util.concurrent.Executors;
importjava.util.concurrent.ScheduledExecutorService;
importjava.util.concurrent.TimeUnit;
/**
*?Java線程:線程池
*
*?@author?馮小衛
*/
publicclassTest?{
publicstaticvoidmain(String[]?args)?{
//?創建一個線程池,它可安排在給定延遲后運行命令或者定期地執行。
ScheduledExecutorService?pool?=?Executors.newScheduledThreadPool(2);
//?創建實現了Runnable接口對象,Thread對象當然也實現了Runnable接口
Thread?t1?=newMyThread();
Thread?t2?=newMyThread();
Thread?t3?=newMyThread();
//?將線程放入池中進行執行
pool.execute(t1);
//?使用延遲執行風格的方法
pool.schedule(t2,1000,?TimeUnit.MILLISECONDS);
pool.schedule(t3,10,?TimeUnit.MILLISECONDS);
//?關閉線程池
pool.shutdown();
}
}
classMyThreadextendsThread?{
@Override
publicvoidrun()?{
System.out.println(Thread.currentThread().getName()?+"正在執行。。。");
}
}
讀者可以嘗試改變Executors.newScheduledThreadPool(2)的參數來得到更多的體驗,當然,讓
[java]view plaincopy
@Override
publicvoidrun()?{
System.out.println(Thread.currentThread().getName()?+"正在執行。。。");
}
變成一個無限循環,你可以得到更多的關于pool.shutdown()的用法。
五:單任務延遲連接池
http://www.cnblogs.com/dolphin0520/p/3932934.html
http://blog.csdn.net/coding_or_coded/article/details/6856014
http://www.cnblogs.com/micrari/p/5634447.html