为了账号安全,请及时绑定邮箱和手机立即绑定

Executors的newSingleThreadExecutor()和newFixedThreadPool(1)有什么区别?

Executors的newSingleThreadExecutor()和newFixedThreadPool(1)有什么区别?

料青山看我应如是 2019-03-01 10:31:38
Executors的newSingleThreadExecutor()和newFixedThreadPool(1)有什么区别? 参考了一些文章,都说newSingleThreadExecutor()主要有两个特性: 能保证执行顺序,先提交的先执行。 当线程执行中出现异常,去创建一个新的线程替换之。 讲到和newFixedThreadPool(1)的区别,都主要指出如下。 http://www.cnblogs.com/richaa...文章中: 和 newFixedThreadPool(1) 的区别在于,如果线程遇到错误中止,它是无法使用替代线程的。 http://blog.csdn.net/vking_wa...文章中: 如果当前线程意外终止,会创建一个新线程继续执行任务,这和我们直接创建线程不同,也和newFixedThreadPool(1)不同。 但经过我的实验(代码如下),得出两者都是一样的,都保证了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); } ); 运行结果: 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 创建newSingleThreadExecutor的源码中,其实是在newFixedThreadPool(1)的基础上包装了FinalizableDelegatedExecutorService,请问下这个究竟有啥用?麻烦帮忙解答。 public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); }
查看完整描述

3 回答

?
GCT1015

TA贡献1827条经验 获得超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了

以上

查看完整回答
反对 回复 2019-03-01
?
慕斯709654

TA贡献1840条经验 获得超5个赞

lalala, i'm the express of nature

查看完整回答
反对 回复 2019-03-01
  • 3 回答
  • 0 关注
  • 1967 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信