java线程池的使用
生活随笔
收集整理的這篇文章主要介紹了
java线程池的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、在java中使用線程池的情況:
1、任務執行時間比較長的時候,前臺只需要在執行就可以,不需阻塞等待結果
2、出現高并發的情況,如果沒有線程池的管理,會撐爆內存,需要任務排隊執行任務
二、線程池的使用:
1、新城池的分類大類有四種,單例,可變,定長,緩存
2、常用的情況就是熱內比較多,而且任務的執行時間比較長的情況,需要排隊執行任務,并且核心線程數量一定,排隊線程不做限制
import org.springframework.beans.factory.annotation.Value;import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit;public class ThreadPoolManage {@Value("corePoolSize")private static int corePoolSize; //核心線程池數量(運行的最少線程)@Value("maxPoolSize")private static int maxPoolSize; //最大線程池數量(運行的最大線程)和隊列容量無關private ThreadPoolManage (){}//單例線程池private static final ExecutorService fixedThreadPool = new ThreadPoolExecutor(corePoolSize,maxPoolSize,0L,TimeUnit.MILLISECONDS,new LinkedBlockingQueue() //新建一個阻塞隊列,任務入隊等待);public static ExecutorService getFixedThreadPool() {return fixedThreadPool;} }
三、線程池的使用:
ExecutorService fixedThreadPool = ThreadPoolManager.getFiedThreadPool();fixedThreadPool.execute(new ReportTask(reportTaskParam,collectionLogService,collectionInfoService,verifyService,odsInboundService));//自定義的實現Runnable接口的任務類
?
轉載于:https://www.cnblogs.com/hackxiyu/p/8760459.html
總結
以上是生活随笔為你收集整理的java线程池的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UVA-10714 Ants---蚂蚁模
- 下一篇: 三周第五次课) 4.5/4.6 磁盘格式