創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買(mǎi)多久送多久,劃算不套路!
成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的臨潭網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!這篇文章將為大家詳細(xì)講解有關(guān)java 中ThreadPoolExecutor的原理是什么,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
java 中ThreadPoolExecutor原理分析
線程池簡(jiǎn)介
Java線程池是開(kāi)發(fā)中常用的工具,當(dāng)我們有異步、并行的任務(wù)要處理時(shí),經(jīng)常會(huì)用到線程池,或者在實(shí)現(xiàn)一個(gè)服務(wù)器時(shí),也需要使用線程池來(lái)接收連接處理請(qǐng)求。
線程池使用
JDK中提供的線程池實(shí)現(xiàn)位于java.util.concurrent.ThreadPoolExecutor。在使用時(shí),通常使用ExecutorService接口,它提供了submit,invokeAll,shutdown等通用的方法。
在線程池配置方面,Executors類中提供了一些靜態(tài)方法能夠提供一些常用場(chǎng)景的線程池,如newFixedThreadPool,newCachedThreadPool,newSingleThreadExecutor等,這些方法最終都是調(diào)用到了ThreadPoolExecutor的構(gòu)造函數(shù)。
ThreadPoolExecutor的包含所有參數(shù)的構(gòu)造函數(shù)是
/** * @param corePoolSize the number of threads to keep in the pool, even * if they are idle, unless {@code allowCoreThreadTimeOut} is set * @param maximumPoolSize the maximum number of threads to allow in the * pool * @param keepAliveTime when the number of threads is greater than * the core, this is the maximum time that excess idle threads * will wait for new tasks before terminating. * @param unit the time unit for the {@code keepAliveTime} argument * @param workQueue the queue to use for holding tasks before they are * executed. This queue will hold only the {@code Runnable} * tasks submitted by the {@code execute} method. * @param threadFactory the factory to use when the executor * creates a new thread * @param handler the handler to use when execution is blocked * because the thread bounds and queue capacities are reached public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueueworkQueue, 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.corePoolSize = corePoolSize; this.maximumPoolSize = maximumPoolSize; this.workQueue = workQueue; this.keepAliveTime = unit.toNanos(keepAliveTime); this.threadFactory = threadFactory; this.handler = handler; }