一、 為什么要使用線程池:
1.頻繁創(chuàng)建和銷毀線程耗資源,耗時間,換句話說,線程池就能節(jié)約資源,節(jié)約時間稽穆。
2.有的線程執(zhí)行任務的時間甚至比創(chuàng)建和銷毀線程的時間還短冠王。
二 、線程池的作用:
線程池是預先創(chuàng)建線程的一種技術舌镶。線程池在還沒有任務到來之前,創(chuàng)建一定數(shù)量的線程,加入空閑隊列中,然后對這些資源進行復用柱彻,減少頻繁的創(chuàng)建和銷毀對象。
線程池涉及的類:
1.Excutor--java里面線程池的接口
2.ExcutorService--真正的線程池接口
3.ScheduledExecutorService(接口)--和Timer/TimerTask類似,解決那些需要重復執(zhí)行的任務.
4.ThreadPoolExcutor(重點)--ExecutorService的默認實現(xiàn).
5.ScheduledThreadPoolExecutor(實現(xiàn)類)--繼承ThreadPoolExecutor,實現(xiàn)ScheduledExecutorService接口--周期性任務調(diào)度的實現(xiàn).
Executors:
JDK1.5之后的一個新類,提供了一些靜態(tài)工廠,生成一些常用的線程池.ThreadPoolExecutor類的底層實現(xiàn).
1.SingleThreadExecutor(單線程)
用法:
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
singleThreadExecutor.execute(runnable對象);
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
創(chuàng)建一個單線程的線程池.這個線程池只有一個線程在工程,也就是相當于單線程串行執(zhí)行所有任務.如果這個唯一的線程因為異常結束,那么會有一個新的線程來替代它.此線程池保證所有任務的執(zhí)行順序都會按照提交的順序執(zhí)行.
** 2.FixedThreadPool(固定線程)**
用法:
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(num);
fixedThreadPool.execute(runnable對象);
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
創(chuàng)建固定大小的線程池.每次提交一個任務就會創(chuàng)建一個線程,直到線程達到線程池的最大大小.線程池的大小一旦達到最大值就會保持不變,如果某個線程因為異常而技術,那么該線程池會補充一個新的線程.
3.CachedThreadPool(帶緩存)
用法:
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
cachedThreadPool.execute(runnable對象);
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
創(chuàng)建一個可緩存的線程池.如果線程池的大小超過了處理任務所需要的線程,那么就會回收部分空閑(60秒不執(zhí)行任務)的線程,當任務數(shù)增加時,此線程池又可以智能的添加新線程來處理任務.此線程池不會對線程池大小做限制,線程池大小完全依賴與操作系統(tǒng)(或者說JVM)能夠創(chuàng)建的最大線程數(shù).
4.ScheduledThreadPool(定時執(zhí)行)
用法:
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(int corePoolSize);
scheduledThreadPool.schedule(runnable對象, 2000, TimeUnit.MILLISECONDS);
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, new DelayedWorkQueue());
}
創(chuàng)建一個大小無限的線程池.此線程池支持定時以及周期性執(zhí)行任務的需求.
線程池管理的工具類餐胀,封裝類哟楷。
通過ThreadPoolExecutor的代理類來對線程池的管理實例
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 線程池管理的工具類,封裝類
* @author ThinkPad
* 線程池的管理 否灾,通過java 中的api實現(xiàn)管理
* 采用conncurrent框架: 非常成熟的并發(fā)框架 卖擅,特別在匿名線程管理非常優(yōu)秀的
*
*/
public class ThreadManager {
//通過ThreadPoolExecutor的代理類來對線程池的管理
private static ThreadPollProxy mThreadPollProxy;
//單列對象
public static ThreadPollProxy getThreadPollProxy(){
synchronized (ThreadPollProxy.class) {
if(mThreadPollProxy==null){
mThreadPollProxy=new ThreadPollProxy(3,6,1000);
}
}
return mThreadPollProxy;
}
//通過ThreadPoolExecutor的代理類來對線程池的管理
public static class ThreadPollProxy{
private ThreadPoolExecutor poolExecutor;//線程池執(zhí)行者 ,java內(nèi)部通過該api實現(xiàn)對線程池管理
private int corePoolSize;
private int maximumPoolSize;
private long keepAliveTime;
public ThreadPollProxy(int corePoolSize,int maximumPoolSize,long keepAliveTime){
this.corePoolSize=corePoolSize;
this.maximumPoolSize=maximumPoolSize;
this.keepAliveTime=keepAliveTime;
}
//對外提供一個執(zhí)行任務的方法
public void execute(Runnable r){
if(poolExecutor==null||poolExecutor.isShutdown()){
poolExecutor=new ThreadPoolExecutor(
//核心線程數(shù)量
corePoolSize,
//最大線程數(shù)量
maximumPoolSize,
//當線程空閑時墨技,保持活躍的時間
keepAliveTime,
//時間單元 磨镶,毫秒級
TimeUnit.MILLISECONDS,
//線程任務隊列
new LinkedBlockingQueue<Runnable>(),
//創(chuàng)建線程的工廠
Executors.defaultThreadFactory());
}
poolExecutor.execute(r);
}
}
}