Java Executors.newFixedThreadPool(1) 和 Executors.newSingleThreadExecutor() 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21300924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Executors.newFixedThreadPool(1) and Executors.newSingleThreadExecutor()
提问by TheLostMind
My question is : does it make sense to use Executors.newFixedThreadPool(1)??
. In two threads (main + oneAnotherThread) scenarios is it efficient to use executor service?. Is creating a new thread directly by calling new Runnable(){ }
better than using ExecutorService?. What are the upsides and downsides of using ExecutorService for such scenarios?
我的问题是:使用Executors.newFixedThreadPool(1)??
. 在两个线程(main + oneAnotherThread)的场景下,使用执行器服务是否高效?通过调用直接创建新线程new Runnable(){ }
比使用 ExecutorService 更好吗?。在这种情况下使用 ExecutorService 有什么好处和坏处?
PS: Main thread and oneAnotherThread dont access any common resource(s).
PS:主线程和 oneAnotherThread 不访问任何公共资源。
I have gone through : What are the advantages of using an ExecutorService?. and Only one thread at a time!
我经历过:使用 ExecutorService 有什么好处?. 并且一次只有一个线程!
采纳答案by assylias
does it make sense to use
Executors.newFixedThreadPool(1)
?
使用有意义
Executors.newFixedThreadPool(1)
吗?
It is essentially the same thing as an Executors.newSingleThreadExecutor()
except that the latter is not reconfigurable, as indicated in the javadoc, whereas the former is if you cast it to a ThreadPoolExecutor
.
它本质上与 anExecutors.newSingleThreadExecutor()
相同,除了后者不可重新配置,如javadoc 所示,而前者是如果您将其强制转换为ThreadPoolExecutor
.
In two threads (main + oneAnotherThread) scenarios is it efficient to use executor service?
在两个线程(main + oneAnotherThread)场景下使用executor服务效率高吗?
An executor service is a very thin wrapper around a Thread that significantly facilitates the thread lifecycle management. If the only thing you need is to new Thread(runnable).start();
and move on, then there is no real need for an ExecutorService.
执行程序服务是围绕线程的非常薄的包装器,可显着促进线程生命周期管理。如果您唯一需要的是继续new Thread(runnable).start();
前进,那么就没有真正需要 ExecutorService。
In any most real life cases, the possibility to monitor the life cycle of the tasks (through the returned Future
s), the fact that the executor will re-create threads as required in case of uncaught exceptions, the performance gain of recycling threads vs. creating new ones etc. make the executor service a much more powerful solution at little additional cost.
在任何最真实的生活案例中,监视任务生命周期的可能性(通过返回的Future
s),在未捕获异常的情况下,执行程序将根据需要重新创建线程的事实,回收线程的性能增益与创建新的等等,使 executor 服务成为一个更强大的解决方案,而几乎没有额外的成本。
Bottom line: I don't see any downsides of using an executor service vs. a thread.
底线:我没有看到使用执行程序服务与线程的任何缺点。
The difference between Executors.newSingleThreadExecutor().execute(command) and new Thread(command).start();goes through the small differences in behaviour between the two options.
Executors.newSingleThreadExecutor().execute(command) 和 new Thread(command).start() 的区别;经历两个选项之间行为的微小差异。
回答by v.ladynev
Sometimes need to use Executors.newFixedThreadPool(1)
to determine number of tasks in the queue
有时需要使用Executors.newFixedThreadPool(1)
来确定队列中的任务数
private final ExecutorService executor = Executors.newFixedThreadPool(1);
public int getTaskInQueueCount() {
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
return threadPoolExecutor.getQueue().size();
}
回答by Ravindra babu
does it make sense to use Executors.newFixedThreadPool(1)??
使用 Executors.newFixedThreadPool(1) 有意义吗??
Yes. It makes sense If you want to process all submitted tasks in order of arrival
是的。如果要按到达顺序处理所有提交的任务,这是有道理的
In two threads (main + oneAnotherThread) scenarios is it efficient to use executor service? Is creating a new thread directly by calling new Runnable(){ } better than using ExecutorService?.
在两个线程(main + oneAnotherThread)场景下使用executor服务效率高吗?通过调用 new Runnable(){ } 直接创建一个新线程比使用 ExecutorService 更好吗?。
I prefer ExecutorService
or ThreadPoolExecutor
even for 1 thread.
我喜欢ExecutorService
或ThreadPoolExecutor
甚至1个线程。
Refer to below SE question for explanation for advantages of ThreadPoolExecutor
over new Runnable()
:
有关ThreadPoolExecutor
over new优点的解释,请参阅以下 SE 问题Runnable()
:
ExecutorService vs Casual Thread Spawner
ExecutorService 与 Casual Thread Spawner
What are the upsides and downsides of using ExecutorService for such scenarios?
在这种情况下使用 ExecutorService 有什么好处和坏处?
Have a look at related SE question regarding ExexutorService use cases :
查看有关 ExexutorService 用例的相关 SE 问题:
Java's Fork/Join vs ExecutorService - when to use which?
Java 的 Fork/Join 与 ExecutorService - 何时使用哪个?
Regarding your query in subject line (from grepcode), both are same:
关于您在主题行中的查询(来自grepcode),两者都是相同的:
newFixedThreadPool
API will return ThreadPoolExecutor as ExecutorService:
newFixedThreadPool
API 将 ThreadPoolExecutor 作为 ExecutorService 返回:
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
and
和
newSingleThreadExecutor()
return ThreadPoolExecutor as ExecutorService:
newSingleThreadExecutor()
将 ThreadPoolExecutor 作为 ExecutorService 返回:
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
I agree with @assylias answer regarding similarities/differences.
我同意@assylias 关于相似性/差异的回答。
回答by KayV
Is creating a new thread directly by calling new Runnable(){ } better than using ExecutorService?
通过调用 new Runnable(){ } 直接创建新线程是否比使用 ExecutorService 更好?
If you want to compute something on the returned result after thread compilation, you can use Callable interface, which can be used with ExecutorService only, not with new Runnable(){}. The ExecutorService's submit() method, which take the Callable object as an arguement, returns the Future object. On this Future object you check whether the task has been completed on not using isDone() method. Also you can get the results using get() method. In this case, ExecutorService is better than the new Runnable(){}.
如果要在线程编译后对返回的结果进行计算,可以使用 Callable 接口,该接口只能与 ExecutorService 一起使用,不能与 new Runnable(){} 一起使用。ExecutorService 的 submit() 方法以 Callable 对象为参数,返回 Future 对象。在此 Future 对象上,您检查任务是否已在不使用 isDone() 方法的情况下完成。您也可以使用 get() 方法获取结果。在这种情况下,ExecutorService 比新的 Runnable(){} 更好。