java java中线程池的类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17186206/
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
Types of Thread Pools in java
提问by Ankit Zalani
What are the types of thread pools in java. I need to implement a robust multi-threaded application which uses heavy computation, which thread pool should I use?
java中线程池的类型有哪些?我需要实现一个强大的多线程应用程序,它使用大量计算,我应该使用哪个线程池?
回答by
There are various thread pools in java:
java中有多种线程池:
Single Thread Executor : A thread pool with only one thread. So all the submitted tasks will be executed sequentially. Method :
Executors.newSingleThreadExecutor()
Cached Thread Pool : A thread pool that creates as many threads it needs to execute the task in parrallel. The old available threads will be reused for the new tasks. If a thread is not used during 60 seconds, it will be terminated and removed from the pool. Method :
Executors.newCachedThreadPool()
Fixed Thread Pool : A thread pool with a fixed number of threads. If a thread is not available for the task, the task is put in queue waiting for an other task to ends. Method :
Executors.newFixedThreadPool()
Scheduled Thread Pool : A thread pool made to schedule future task. Method :
Executors.newScheduledThreadPool()
Single Thread Scheduled Pool : A thread pool with only one thread to schedule future task. Method :
Executors.newSingleThreadScheduledExecutor()
单线程执行器:只有一个线程的线程池。所以所有提交的任务都会依次执行。方法 :
Executors.newSingleThreadExecutor()
缓存线程池:一个线程池,它创建并行执行任务所需的尽可能多的线程。旧的可用线程将重新用于新任务。如果一个线程在 60 秒内未被使用,它将被终止并从池中删除。方法 :
Executors.newCachedThreadPool()
固定线程池:具有固定线程数的线程池。如果某个线程不可用于该任务,则该任务将被放入队列中等待另一个任务结束。方法 :
Executors.newFixedThreadPool()
调度线程池:用于调度未来任务的线程池。方法 :
Executors.newScheduledThreadPool()
单线程调度池:只有一个线程来调度未来任务的线程池。方法 :
Executors.newSingleThreadScheduledExecutor()
回答by fge
There are many types ;)
有很多种;)
There is, for instance, ExecutorService
. This is the "basic" implementation which allows to submit tasks etc. You will probably want to use Executors
to obtain a new one, since it has static factory methods for the most common scenarios.
例如,有ExecutorService
。这是允许提交任务等的“基本”实现。您可能希望使用它Executors
来获取新的实现,因为它具有适用于最常见场景的静态工厂方法。
Since Java 7 you also have ForkJoinPool
.
从 Java 7 开始,您还拥有ForkJoinPool
.
Also have a look at FutureTask
, since this is a very convenient class to build individual threads.
也看看FutureTask
,因为这是一个非常方便的类来构建单独的线程。
回答by Viktor Mellgren
This shows good animations on the diffrent concurrency constructs, may this will help you choose
这在不同的并发构造上显示了很好的动画,这可能会帮助您选择
回答by omer schleifer
You can read more about ThreadPoolExecutors here: http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html
您可以在此处阅读有关 ThreadPoolExecutors 的更多信息:http: //docs.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html
However, it might be a good idea to look at the ForkJoinTask API: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ForkJoinTask.html
但是,查看 ForkJoinTask API 可能是个好主意:http: //docs.oracle.com/javase/7/docs/api/java/util/concurrent/ForkJoinTask.html