执行程序优于新线程的优点 [英] Advantages of Executors over new Thread
问题描述
在Java程序中使用执行程序而不仅仅是线程有什么好处。
What benefit is there to use Executors over just Threads in a Java program.
例如
ExecutorService pool = Executors.newFixedThreadPool(2);
void someMethod() {
//Thread
new Thread(new SomeRunnable()).start();
//vs
//Executor
pool.execute(new SomeRunnable());
}
执行者是否只限制允许一次运行的线程数(线程池)?它是否实际上将runnable复用到它创建的线程上?如果不是,它只是一种避免每次都必须编写新的Thread(runnable).start()的方法吗?
Does an executor just limit the number of threads it allows to have running at once (Thread Pooling)? Does it actually multiplex runnables onto the threads it creates instead? If not is it just a way to avoid having to write new Thread(runnable).start() every time?
推荐答案
是执行程序通常会将runnables多路复用到它们创建的线程上;他们会限制并管理一次运行的线程数量;他们会更容易定制并发级别。通常,执行程序应该优先于创建裸线程。
Yes, executors will generally multiplex runnables onto the threads they create; they'll constrain and manage the number of threads running at once; they'll make it much easier to customize concurrency levels. Generally, executors should be preferred over just creating bare threads.
这篇关于执行程序优于新线程的优点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!