線程池為線程生命周期開銷問題和資源不足問題提供了解決方案。通過對多個任務重用線程,線程創建的開銷被分攤到了多個任務上。其好處是,因為在請求到達時線程已經存在,所以無意中也消除了線程創建所帶來的延遲。這樣,就可以立即為請求服務,使應用程序響應更快。而且,通過適當地調整線程池中的線程數目,也就是當請求的數目超過某個閾值時,就強制其它任何新到的請求一直等待,直到獲得一個線程來處理為止,從而可以防止資源不足。
具體的線程池詳細見解 如: http://www.importnew.com/19011.html
使用spring管理線程池的使用
1、創建線程池的配置信息threads.properties
####業務線程池配置#### #是否啟用自定義線程池。true時啟動,以下參數生效 handler.threads.custom=false #核心線程數 handler.threads.corePoolSize=20 #最大線程數 handler.threads.maximumPoolSize=1000 #空閑線程存活時間,單位秒 handler.threads.keepAliveTime=100 #工作隊列大小,為0是無限大 handler.threads.workQueue=0
2、創建 線程池 配置,ThreadsPoolConfig.java
package com.hk.core.concurrent; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * 線程池 配置 */ @Component public class ThreadsPoolConfig { /** * 是否開啟自定義線程池 */ @Value("${handler.threads.custom}") private boolean custom; /** * 核心線程數 */ @Value("${handler.threads.corePoolSize}") private int corePoolSize; /** * 線程池最大線程數 */ @Value("${handler.threads.maximumPoolSize}") private int maximumPoolSize; /** * 空閑線程存活時間(對核心線程無效) */ @Value("${handler.threads.keepAliveTime}") private long keepAliveTime; /** * 任務隊列大小,0時為無界隊列 */ @Value("${handler.threads.workQueue}") private int workQueue; public boolean isCustom() { return custom; } public void setCustom(boolean custom) { this.custom = custom; } public int getCorePoolSize() { return corePoolSize; } public void setCorePoolSize(int corePoolSize) { this.corePoolSize = corePoolSize; } public int getMaximumPoolSize() { return maximumPoolSize; } public void setMaximumPoolSize(int maximumPoolSize) { this.maximumPoolSize = maximumPoolSize; } public long getKeepAliveTime() { return keepAliveTime; } public void setKeepAliveTime(long keepAliveTime) { this.keepAliveTime = keepAliveTime; } public int getWorkQueue() { return workQueue; } public void setWorkQueue(int workQueue) { this.workQueue = workQueue; } }
3、創建 線程池 處理器管理線程 HandlerThreadsPool.java
package com.hk.core.concurrent; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import javax.annotation.PreDestroy; /** * 線程管理器 */ public class HandlerThreadsPool { public ExecutorService executorService; public HandlerThreadsPool() { // TODO Auto-generated constructor stub this.executorService=Executors.newCachedThreadPool(); } public HandlerThreadsPool(ThreadsPoolConfig config) { // TODO Auto-generated constructor stub if(config.isCustom()){ BlockingQueue<Runnable> queue=null; if(config.getWorkQueue()>0){ queue=new LinkedBlockingQueue<Runnable>(config.getWorkQueue()); // 一般使用 LinkedBlockingQueue 隊列 }else{ queue=new LinkedBlockingQueue<Runnable>(); }
// 配置線程池信息 this.executorService=new ThreadPoolExecutor( config.getCorePoolSize(), config.getMaximumPoolSize(), config.getKeepAliveTime(), TimeUnit.SECONDS, queue, new ThreadPoolExecutor.AbortPolicy()//拒絕策略,任務隊列滿后,新的任務將被丟棄,並拋出異常 ); }else{ this.executorService=Executors.newCachedThreadPool(); } }
/*
* 創建線程,對線程處理事件
*/ public void execute(Runnable runnable){ executorService.execute(runnable); } /*
* 對象銷毀時,銷毀線程
*/
@PreDestroy public void stop() { executorService.shutdown(); } }
4、使用線程池
package com.hk.core.concurrent; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class MsgHandler implements Runnable{ @Autowired private ThreadsPoolConfig config; // 注入 配置 @Override public void run() { // do 這里 寫 處理的邏輯 System.out.println("創建線程 處理事務...."); } @PostConstruct public void loadThreadsPool(){ // 初始化 線程池 HandlerThreadsPool handlerThreadsPool=new HandlerThreadsPool(config); //調用線程池,創建線程 。處理事件 handlerThreadsPool.execute(new MsgHandler()); } }
簡單的例子就這樣完美使用線程池了
