Types of Thread Pools in java [closed]

2019-03-09 12:46发布

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?

5条回答
Luminary・发光体
2楼-- · 2019-03-09 13:35

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.

Since Java 7 you also have ForkJoinPool.

Also have a look at FutureTask, since this is a very convenient class to build individual threads.

查看更多
霸刀☆藐视天下
3楼-- · 2019-03-09 13:38

Take a look at Executors.

Each common ExecutorService is explained and you will probably find one that fits your needs among them.

查看更多
淡お忘
4楼-- · 2019-03-09 13:39

You can read more about ThreadPoolExecutors here: 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

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-03-09 13:46

There are various thread pools in 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()

查看更多
疯言疯语
6楼-- · 2019-03-09 13:48

This shows good animations on the diffrent concurrency constructs, may this will help you choose

http://sourceforge.net/projects/javaconcurrenta/

查看更多
登录 后发表回答