java自帶的線程池方法
大小:0.3 MB 人氣: 2017-09-27 需要積分:1
推薦 + 挑錯(cuò) + 收藏(0) + 用戶評(píng)論(0)
標(biāo)簽:JAVA(102450)線程(19385)
二、原理分析從上面使用線程池的例子來(lái)看,最主要就是兩步,構(gòu)造ThreadPoolExecutor對(duì)象,然后每來(lái)一個(gè)任務(wù),就調(diào)用ThreadPoolExecutor對(duì)象的execute方法。
1、ThreadPoolExecutor結(jié)構(gòu)
ThreadPoolExecutor的主要結(jié)構(gòu)及繼承關(guān)系如下圖所示:
主要成員變量:任務(wù)隊(duì)列——存放那些暫時(shí)無(wú)法執(zhí)行的任務(wù);工作線程池——存放當(dāng)前啟用的所有線程;線程工廠——?jiǎng)?chuàng)建線程;還有一些用來(lái)調(diào)度線程與任務(wù)并保證線程安全的成員。
了解了ThreadPoolExecutor的主要結(jié)構(gòu),再簡(jiǎn)單梳理一下“一個(gè)傳入線程池的任務(wù)能夠被最終正常執(zhí)行需要經(jīng)過(guò)的主要流程”,方法名稱前面沒(méi)有“XXX.”這種標(biāo)注的都是ThreadPoolExecutor的方法:
2、ThreadPoolExecutor構(gòu)造器及重要常量
簡(jiǎn)單了解下構(gòu)造器,ThreadPoolExecutor的四個(gè)構(gòu)造器的源碼如下:
publicThreadPoolExecutor( intcorePoolSize, intmaximumPoolSize,longkeepAliveTime,TimeUnit unit,BlockingQueue《Runnable》 workQueue) {this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,Executors.defaultThreadFactory(), defaultHandler); }publicThreadPoolExecutor( intcorePoolSize, intmaximumPoolSize,longkeepAliveTime,TimeUnit unit,BlockingQueue《Runnable》 workQueue,ThreadFactory threadFactory) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,threadFactory, defaultHandler); } publicThreadPoolExecutor( intcorePoolSize,intmaximumPoolSize, longkeepAliveTime,TimeUnit unit,BlockingQueue《Runnable》 workQueue,RejectedExecutionHandler handler) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,Executors.defaultThreadFactory(), handler); }publicThreadPoolExecutor( intcorePoolSize, intmaximumPoolSize,longkeepAliveTime,TimeUnit unit,BlockingQueue《Runnable》 workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler) { if(corePoolSize 《0||maximumPoolSize 《= 0||maximumPoolSize 《 corePoolSize ||keepAliveTime 《 0)thrownewIllegalArgumentException(); if(workQueue == null|| threadFactory == null|| handler == null) thrownewNullPointerException(); 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; }
從源碼中可以看出,這四個(gè)構(gòu)造器都是調(diào)用最后一個(gè)構(gòu)造器,只是根據(jù)開(kāi)發(fā)者傳入的參數(shù)的不同而填充一些默認(rèn)的參數(shù)。比如如果開(kāi)發(fā)者沒(méi)有傳入線程工廠threadFactory參數(shù),那么構(gòu)造器就使用默認(rèn)的Executors.defaultThreadFactor。
在這里還要理解ThreadPoolExecutor的幾個(gè)常量的含義和幾個(gè)簡(jiǎn)單方法:
//Integer.SIZE是一個(gè)靜態(tài)常量,值為32,也就是說(shuō)COUNT_BITS是29privatestaticfinalintCOUNT_BITS = Integer.SIZE - 3; //CAPACITY是最大容量536870911,因?yàn)?左移29位之后-1,導(dǎo)致最高三位為0,而其余29位全部為1privatestaticfinalintCAPACITY = ( 1《《 COUNT_BITS) - 1; //ctl用于表示線程池狀態(tài)和有效線程數(shù)量,最高三位表示線程池的狀態(tài),其余低位表示有效線程數(shù)量//初始化之后ctl等于RUNNING的值,即默認(rèn)狀態(tài)是運(yùn)行狀態(tài),線程數(shù)量為0privatefinalAtomicInteger ctl =newAtomicInteger(ctlOf(RUNNING, 0)); //1110 0000 0000 0000 0000 0000 0000 0000最高三位為111privatestaticfinalintRUNNING = - 1《《 COUNT_BITS; //最高三位為000privatestaticfinalintSHUTDOWN = 0《《 COUNT_BITS; //0010 0000 0000 0000 0000 0000 0000 0000最高三位為001privatestaticfinalintSTOP = 1《《 COUNT_BITS; //0100 0000 0000 0000 0000 0000 0000 0000最高三位為010privatestaticfinalintTIDYING = 2《《 COUNT_BITS; //0110 0000 0000 0000 0000 0000 0000 0000最高三位為011privatestaticfinalintTERMINATED = 3《《 COUNT_BITS; /** * 獲取運(yùn)行狀態(tài),入?yún)閏tl。因?yàn)镃APACITY是最高三位為0,其余低位為1 * 所以當(dāng)取反的時(shí)候,就只有最高三位為1,再經(jīng)過(guò)與運(yùn)算,只會(huì)取到ctl的最高三位 * 而這最高三位如上文所述,表示線程池的狀態(tài) */privatestaticintrunStateOf( intc) { returnc & ~CAPACITY; } /** * 獲取工作線程數(shù)量,入?yún)閏tl。因?yàn)镃APACITY是最高三位為0,其余低位為1 * 所以當(dāng)進(jìn)行與運(yùn)算的時(shí)候,只會(huì)取到低29位,這29位正好表示有效線程數(shù)量 */privatestaticintworkerCountOf( intc) { returnc & CAPACITY; } privatestaticintctlOf( intrs, intwc) { returnrs | wc; } //任務(wù)隊(duì)列,用于存放待執(zhí)行任務(wù)的阻塞隊(duì)列privatefinalBlockingQueue《Runnable》 workQueue; /** 判斷線程池是否是運(yùn)行狀態(tài),傳入的參數(shù)是ctl的值 * 只有RUNNING的符號(hào)位是1,也就是只有RUNNING為負(fù)數(shù) * 所以如果目前的ctl值《0,就是RUNNING狀態(tài) */privatestaticbooleanisRunning(intc) { returnc 《 SHUTDOWN; } //從任務(wù)隊(duì)列中移除任務(wù)publicbooleanremove(Runnable task) { booleanremoved = workQueue.remove(task); tryTerminate(); // In case SHUTDOWN and now emptyreturnremoved; }
非常好我支持^.^
(0) 0%
不好我反對(duì)
(0) 0%
下載地址
java自帶的線程池方法下載
相關(guān)電子資料下載
- 一文詳解ZGC關(guān)鍵技術(shù) 26
- AMD推出銳龍 Threadripper 7000系列處理器 171
- 如何使用pthread_barrier_xxx系列函數(shù)來(lái)實(shí)現(xiàn)多線程之間的同步? 29
- SpringBoot物理線程、虛擬線程、Webflux性能比較 37
- Rust語(yǔ)言為什么這么卷? 21
- 什么是分布式鎖 Redis的五種分布式鎖方案 32
- SV線程的使用和控制 121
- Python 如何獲取旅游景點(diǎn)信息 82
- Guava中這些Map的操作,讓我的代碼量減少了50% 70
- java實(shí)現(xiàn)定時(shí)器的四種方式 83