衡阳派盒市场营销有限公司

您好,歡迎來(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ī)定!

      ?
      大发888新址| 赌百家乐官网的体会| bet365国际娱乐| 百家乐秘籍下注法| 百家乐筹码片| 威尼斯人娱乐城玩百家乐| 赌博百家乐趋势把握| 百家乐群html| 百家乐官网开和几率| 百家乐官网稳赢秘诀教学| 真人百家乐官网最高赌注| 真人百家乐海立方| 百家乐现金网排名| 百家乐软件购买| 百家乐太阳城娱乐城| 百家乐网络赌博地址| 百家乐任你博赌场娱乐网规则| 百家乐赌术大揭秘| 百家乐心得打法| 大发888dafabet| 赌博| 百家乐官网澳门色子| 带百家乐官网的时时彩平台| 蓝盾百家乐官网平台租用| 百家乐官网赌场彩| 百家乐官网网哪一家做的最好呀| 百家乐永利娱乐城| 百家乐德州扑克桌布| 德州扑克筹码| 家百家乐官网破解软件| 百家乐官网送现金200| 澳门百家乐游戏皇冠网| 新加坡百家乐规则| 凤凰网上娱乐| 澳门百家乐官网新濠天地| 百家乐为什么庄5| 全讯网qx1860| 六合彩查询| 网上真钱麻将游戏| 怎样赢百家乐官网的玩法技巧和规则| 百家乐赌场大赢家|