乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      深入理解Java之線程池(下)

       月冷星河 2016-04-24

      接上文


      下面是這三個(gè)靜態(tài)方法的具體實(shí)現(xiàn);


      public static ExecutorService newFixedThreadPool(int nThreads) {

          return new ThreadPoolExecutor(nThreads, nThreads,

                                        0L, TimeUnit.MILLISECONDS,

                                        new LinkedBlockingQueue());

      }

      public static ExecutorService newSingleThreadExecutor() {

          return new FinalizableDelegatedExecutorService

              (new ThreadPoolExecutor(1, 1,

                                      0L, TimeUnit.MILLISECONDS,

                                      new LinkedBlockingQueue()));

      }

      public static ExecutorService newCachedThreadPool() {

          return new ThreadPoolExecutor(0, Integer.MAX_VALUE,

                                        60L, TimeUnit.SECONDS,

                                        new SynchronousQueue());

      }


      從它們的具體實(shí)現(xiàn)來(lái)看,它們實(shí)際上也是調(diào)用了ThreadPoolExecutor,只不過(guò)參數(shù)都已配置好了。


      newFixedThreadPool創(chuàng)建的線程池corePoolSize和maximumPoolSize值是相等的,它使用的LinkedBlockingQueue;


      newSingleThreadExecutor將corePoolSize和maximumPoolSize都設(shè)置為1,也使用的LinkedBlockingQueue;


      newCachedThreadPool將corePoolSize設(shè)置為0,將maximumPoolSize設(shè)置為Integer.MAX_VALUE,使用的SynchronousQueue,也就是說(shuō)來(lái)了任務(wù)就創(chuàng)建線程運(yùn)行,當(dāng)線程空閑超過(guò)60秒,就銷(xiāo)毀線程。


      實(shí)際中,如果Executors提供的三個(gè)靜態(tài)方法能滿(mǎn)足要求,就盡量使用它提供的三個(gè)方法,因?yàn)樽约喝ナ謩?dòng)配置ThreadPoolExecutor的參數(shù)有點(diǎn)麻煩,要根據(jù)實(shí)際任務(wù)的類(lèi)型和數(shù)量來(lái)進(jìn)行配置。


      另外,如果ThreadPoolExecutor達(dá)不到要求,可以自己繼承ThreadPoolExecutor類(lèi)進(jìn)行重寫(xiě)。


      四.如何合理配置線程池的大小


      本節(jié)來(lái)討論一個(gè)比較重要的話(huà)題:如何合理配置線程池大小,僅供參考。

      一般需要根據(jù)任務(wù)的類(lèi)型來(lái)配置線程池大?。?/p>


      如果是CPU密集型任務(wù),就需要盡量壓榨CPU,參考值可以設(shè)為 NCPU+1

      如果是IO密集型任務(wù),參考值可以設(shè)置為2*NCPU


      當(dāng)然,這只是一個(gè)參考值,具體的設(shè)置還需要根據(jù)實(shí)際情況進(jìn)行調(diào)整,比如可以先將線程池大小設(shè)置為參考值,再觀察任務(wù)運(yùn)行情況和系統(tǒng)負(fù)載、資源利用率來(lái)進(jìn)行適當(dāng)調(diào)整。


      參考資料:


      http:///java-threadpool/


      http://blog.163.com/among_1985/blog/static/275005232012618849266/


      http://developer.51cto.com/art/201203/321885.htm


      http://blog.csdn.net/java2000_wl/article/details/22097059


      http://blog.csdn.net/cutesource/article/details/6061229


      http://blog.csdn.net/xieyuooo/article/details/8718741


      《JDK API 1.6》


        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多