返回信息流面试问到了这个问题,当fixed的线程池size=1的时候,和single有什么区别?
自我感觉没有什么区别,就去看了源码
在源码对newSingleThreadExecutor注释里看到这句:
Unlike the otherwise equivalent{@code newFixedThreadPool(1)} the returned executor is guaranteed not to be reconfigurable to use additional threads.
没看懂,求大神解答
这是一条镜像帖。来源:北邮人论坛 / java / #56933同步于 2017/8/2
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
【问题】fixed 和 single的线程池的区别
homeless271
2017/8/2镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
赞楼主分享精神。以前没想过这个问题。打开api看了一下,发现方法命名规则都不一样,newSingleThreadExecutor和newFixedThreadPool,细想也有道理,单线程的已经不能叫pool了,他们就给换了个名字,叫executor更恰当,因吹斯听。
ls也翻译了,SingleThreadExecutor不可动态配置。那来看一下可动态配置怎么用。
ThreadPoolExecutor fixed = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);
fixed.setCorePoolSize(3);
fixed.setMaximumPoolSize(3);
线程池在初始化之后还可配置。
照猫画虎,再试试
ThreadPoolExecutor fixed = (ThreadPoolExecutor) Executors.newSingleThreadExecutor();
fixed.setCorePoolSize(3);
fixed.setMaximumPoolSize(3);
抛异常了,java.util.concurrent.Executors$FinalizableDelegatedExecutorService cannot be cast to java.util.concurrent.ThreadPoolExecutor
回到newSingleThreadExecutor方法源码,它内部生成的ThreadPoolExecutor被FinalizableDelegatedExecutorService包裹起来,屏蔽了ThreadPoolExecutor中的几种set方法,所以不可动态配置。
以上
【 在 homeless271 的大作中提到: 】
: 面试问到了这个问题,当fixed的线程池size=1的时候,和single有什么区别?
: 自我感觉没有什么区别,就去看了源码
: 在源码对newSingleThreadExecutor注释里看到这句:
: ...................
`Executors.newFixedThreadPool(1);`调用的是如下方法
```Java
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
```
`Executors.newSingleThreadExecutor();`调用的是如下方法
```Java
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
```
再来看一下`ThreadPoolExecutor`的构造方法
```Java
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
```
1. __corePoolSize__:核心线程数量,线程池中的最小线程数量。当然初始化的时候线程数量0,一旦达到这个数量之后,就不会再下降了(如果把allowCoreThreadTimeOut字段设置为true,那么核心线程也是会超时结束的)
1. __maximumPoolSize__:最大线程数量,其中超过核心线程数量的部分在没有任务可执行的时候会超时结束
1. __keepAliveTime__:非核心线程数量阻塞的最长时间(没有任务可执行),当线程阻塞时间超过这个数值的时候,线程就会结束了
1. __unit__:keepAliveTime的单位
1. __workQueue__:BlockingQueue
所以我认为`Executors.newFixedThreadPool(1);`与`Executors.newSingleThreadExecutor();`是一样的?
看了2楼的回复,`newSingleThreadExecutor`生成的`ThreadPoolExecutor`被封装成了`FinalizableDelegatedExecutorService`。之前没注意过= =!,学习了。`FinalizableDelegatedExecutorService`是一个非public的类,我们只能当成是`AbstractExecutorService`或者`ExecutorService`来使用,就屏蔽了`ThreadPoolExecutor`的其他方法
【 在 homeless271 的大作中提到: 】
: 面试问到了这个问题,当fixed的线程池size=1的时候,和single有什么区别?
: 自我感觉没有什么区别,就去看了源码
: 在源码对newSingleThreadExecutor注释里看到这句:
: ...................
真心牛,谢谢大神~~以后有事情多向大神请教
【 在 mrcuber 的大作中提到: 】
: 赞楼主分享精神。以前没想过这个问题。打开api看了一下,发现方法命名规则都不一样,newSingleThreadExecutor和newFixedThreadPool,细想也有道理,单线程的已经不能叫pool了,他们就给换了个名字,叫executor更恰当,因吹斯听。
: ls也翻译了,SingleThreadExecutor不可动态配置。那来看一下可动态配置怎么用。
: [code=java]
: ...................