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