那曲檬骨新材料有限公司

您好,歡迎來(lái)電子發(fā)燒友網(wǎng)! ,新用戶?[免費(fèi)注冊(cè)]

您的位置:電子發(fā)燒友網(wǎng)>源碼下載>java源碼下載>

java自帶的線程池方法

大小:0.3 MB 人氣: 2017-09-27 需要積分:1
二、原理分析
  從上面使用線程池的例子來(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%

      發(fā)表評(píng)論

      用戶評(píng)論
      評(píng)價(jià):好評(píng)中評(píng)差評(píng)

      發(fā)表評(píng)論,獲取積分! 請(qǐng)遵守相關(guān)規(guī)定!

      ?
      全讯网qtqnet| 红宝石百家乐的玩法技巧和规则 | 威尼斯人娱乐网代理| 联众百家乐官网的玩法技巧和规则 | 百家乐作弊内幕| 至尊百家乐官网下载| 棋牌游戏大厅| 赌百家乐可以赢钱| 在线百家乐官网纸牌| 长春市| 凱旋門百家乐官网娱乐城| 百家乐官网遥控牌靴| 沈阳盛京棋牌下载| 利来百家乐的玩法技巧和规则| 百家乐色子玩法| 大发888 大发娱乐城| 免费玩百家乐的玩法技巧和规则 | 老虎机批发| 百家乐赌博讨论群| 在线百家乐娱乐| 打百家乐官网庄闲的技巧| 普陀区| 德州扑克单机版| 吴川市| 大发888 dafa888 大发官网| 合乐8百家乐娱乐城| 百家乐编单短信接收| 百家乐星级游戏| 百家乐官网是娱乐场| 澳门百家乐娱乐城送体验金| 风水24向| 立博百家乐官网的玩法技巧和规则| 百家乐官网园sun811.com| 澳门百家乐官网真人娱乐城| 打百家乐官网的技巧| 打百家乐官网的技巧| 百家乐官网小77论坛| 百家乐官网游戏真钱游戏| 百家乐官网全自动分析软件| 东城区| 澳门百家乐官网是骗人的|