亚洲精品久久久久久第一页-人妻少妇精彩视品一区二区三区-91国产自拍免费视频-免费一级a在线播放视频正片-少妇天天日天天射天天爽-国产大屁股喷水视频在线观看-操美女骚穴抽插性爱视频-亚洲 欧美 中文字幕 丝袜-成人免费无码片在线观看

創(chuàng)建線程池的三種方法 自定義線程池拒絕策略


創(chuàng)建線程池的三種方法 自定義線程池拒絕策略

文章插圖
在Java的高并發(fā)領域,線程池一直是一個繞不開的話題 。有些童鞋一直在使用線程池,但是,對于如何創(chuàng)建線程池僅僅停留在使用Executors工具類的方式,那么,創(chuàng)建線程池究竟存在哪幾種方式呢?就讓我們一起從創(chuàng)建線程池的源碼來深入分析究竟有哪些方式可以創(chuàng)建線程池 。
使用Executors工具類創(chuàng)建線程池在創(chuàng)建線程池時,初學者用的最多的就是Executors 這個工具類,而使用這個工具類創(chuàng)建線程池時非常簡單的,不需要關注太多的線程池細節(jié),只需要傳入必要的參數即可 。Executors 工具類提供了幾種創(chuàng)建線程池的方法,如下所示 。
Executors.newCachedThreadPool:創(chuàng)建一個可緩存的線程池,如果線程池的大小超過了需要,可以靈活回收空閑線程,如果沒有可回收線程,則新建線程Executors.newFixedThreadPool:創(chuàng)建一個定長的線程池,可以控制線程的最大并發(fā)數,超出的線程會在隊列中等待Executors.newScheduledThreadPool:創(chuàng)建一個定長的線程池,支持定時、周期性的任務執(zhí)行Executors.newSingleThreadExecutor:創(chuàng)建一個單線程化的線程池,使用一個唯一的工作線程執(zhí)行任務,保證所有任務按照指定順序(先入先出或者優(yōu)先級)執(zhí)行Executors.newSingleThreadScheduledExecutor:創(chuàng)建一個單線程化的線程池,支持定時、周期性的任務執(zhí)行Executors.newWorkStealingPool:創(chuàng)建一個具有并行級別的work-stealing線程池其中,Executors.newWorkStealingPool方法是Java 8中新增的創(chuàng)建線程池的方法,它能夠為線程池設置并行級別,具有更高的并發(fā)度和性能 。除了此方法外,其他創(chuàng)建線程池的方法本質上調用的是ThreadPoolExecutor類的構造方法 。
例如,我們可以使用如下代碼創(chuàng)建線程池 。
Executors.newWorkStealingPool();Executors.newCachedThreadPool();Executors.newScheduledThreadPool(3);使用ThreadPoolExecutor類創(chuàng)建線程池從代碼結構上看ThreadPoolExecutor類繼承自AbstractExecutorService,也就是說,ThreadPoolExecutor類具有AbstractExecutorService類的全部功能 。
既然Executors工具類中創(chuàng)建線程池大部分調用的都是ThreadPoolExecutor類的構造方法,所以,我們也可以直接調用ThreadPoolExecutor類的構造方法來創(chuàng)建線程池,而不再使用Executors工具類 。接下來,我們一起看下ThreadPoolExecutor類的構造方法 。
ThreadPoolExecutor類中的所有構造方法如下所示 。
public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue) {this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler);}public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory) {this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, defaultHandler);}public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue,RejectedExecutionHandler handler) {this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), handler);}public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler) {if (corePoolSize < 0 ||maximumPoolSize <= 0 ||maximumPoolSize < corePoolSize ||keepAliveTime < 0)throw new IllegalArgumentException();if (workQueue == null || threadFactory == null || handler == null)throw new NullPointerException();this.acc = System.getSecurityManager() == null ?null :AccessController.getContext();this.corePoolSize = corePoolSize;this.maximumPoolSize = maximumPoolSize;this.workQueue = workQueue;this.keepAliveTime = unit.toNanos(keepAliveTime);this.threadFactory = threadFactory;this.handler = handler;}由ThreadPoolExecutor類的構造方法的源代碼可知,創(chuàng)建線程池最終調用的構造方法如下 。


以上關于本文的內容,僅作參考!溫馨提示:如遇健康、疾病相關的問題,請您及時就醫(yī)或請專業(yè)人士給予相關指導!

「愛刨根生活網」www.malaban59.cn小編還為您精選了以下內容,希望對您有所幫助: