第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Executors的newSingleThreadExecutor()和newFixedThreadPool(1)有什么區(qū)別?

Executors的newSingleThreadExecutor()和newFixedThreadPool(1)有什么區(qū)別?

Executors的newSingleThreadExecutor()和newFixedThreadPool(1)有什么區(qū)別? 參考了一些文章,都說newSingleThreadExecutor()主要有兩個特性: 能保證執(zhí)行順序,先提交的先執(zhí)行。 當(dāng)線程執(zhí)行中出現(xiàn)異常,去創(chuàng)建一個新的線程替換之。 講到和newFixedThreadPool(1)的區(qū)別,都主要指出如下。 http://www.cnblogs.com/richaa...文章中: 和 newFixedThreadPool(1) 的區(qū)別在于,如果線程遇到錯誤中止,它是無法使用替代線程的。 http://blog.csdn.net/vking_wa...文章中: 如果當(dāng)前線程意外終止,會創(chuàng)建一個新線程繼續(xù)執(zhí)行任務(wù),這和我們直接創(chuàng)建線程不同,也和newFixedThreadPool(1)不同。 但經(jīng)過我的實驗(代碼如下),得出兩者都是一樣的,都保證了1和2。 // ExecutorService executorService = Executors.newSingleThreadExecutor(); ExecutorService executorService = Executors.newFixedThreadPool(1); executorService.execute( () -> { if (count == 100) { thrownewIllegalStateException("handler exception"); } System.out.println(Thread.currentThread()+" - testAsyncRunner1 run ... "+count); } ); 運行結(jié)果: Thread[pool-1-thread-1,5,main] - testAsyncRunner1 run ... 99 Exception in thread "pool-1-thread-1" java.lang.IllegalStateException: handler exception at com.mxx.meal.common.core.async.AsyncHandlerFactoryTest.lambda$null$0(AsyncHandlerFactoryTest.java:32) at com.mxx.meal.common.core.async.AsyncHandlerFactoryTest$$Lambda$2/830539025.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Thread[pool-1-thread-2,5,main] - testAsyncRunner1 run ... 101 創(chuàng)建newSingleThreadExecutor的源碼中,其實是在newFixedThreadPool(1)的基礎(chǔ)上包裝了FinalizableDelegatedExecutorService,請問下這個究竟有啥用?麻煩幫忙解答。 public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); }
查看完整描述

3 回答

?
GCT1015

TA貢獻(xiàn)1827條經(jīng)驗 獲得超4個贊

     //  Unlike the otherwise equivalent
     // {@code newFixedThreadPool(1)} the returned executor is
     // guaranteed not to be reconfigurable to use additional threads.
      public static ExecutorService newSingleThreadExecutor() 

這是jdk8源碼上面的注釋,大體上的意思是 newSingleThreadExecutor 不能配置去重新加入線程;
接下來 舉個栗子

    // final ExecutorService single = Executors.newSingleThreadExecutor();
       final ExecutorService fixed = Executors.newFixedThreadPool(1);
        ThreadPoolExecutor executor = (ThreadPoolExecutor) fixed;
        executor.setCorePoolSize(4);

就是上面的意思
為什么呢?

 /**
     * A wrapper class that exposes only the ExecutorService methods
     * of an ExecutorService implementation.
     */
    static class DelegatedExecutorService extends AbstractExecutorService 

看上面的代碼 FinalizableDelegatedExecutorService extends DelegatedExecutorService,而DelegatedExecutorService的主要作用就是不暴露那么多方法,不讓你配置線程池,至此,single就真的single了

以上

查看完整回答
反對 回復(fù) 2019-03-01
?
慕斯709654

TA貢獻(xiàn)1840條經(jīng)驗 獲得超5個贊

lalala, i'm the express of nature

查看完整回答
反對 回復(fù) 2019-03-01
  • 3 回答
  • 0 關(guān)注
  • 2019 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號