Java.util.concurrent Executors: Difference between revisions
No edit summary |
|||
Line 1: | Line 1: | ||
=External= | |||
* https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html | |||
* https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executor.html | |||
* https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html | |||
=Internal= | =Internal= | ||
Revision as of 13:35, 14 June 2017
External
- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html
- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executor.html
- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html
Internal
Overview
An executor is a mechanism that allows executing tasks on a thread different than the one that submits the task for execution. Executors are preferred to explicitly creating Thread instances. The executor decouples the task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc.
The essential interface is java.util.concurrent.Executor, which defines an object that executes submitted Runnable tasks. Note that the Executor interface does not strictly require that execution be asynchronous - in the simplest case the executor can run the submitted Runnable immediately, in the caller's thread - though asynchronous execution in some other thread than the caller's is generally the case.
java.util.concurrent.ExecutorService extends java.util.concurrent.Executor and allows the instance implementing it to be shut down, which will cause it to reject new tasks. Upon termination, an executor service instance has no tasks actively executing, no tasks awaiting execution, and no new tasks can be submitted.
The package offers an executor instance factory class java.util.concurrent.Executors, which is in general recommended to be used when a new executor is needed.
Typical Usage
int threadCount = 10; ExecutorService executor = Executors.newFixedThreadPool(threadCount); ... executor.execute(new Runtime() { public void run() { ... } });
The ExecutorService so created has a fixed number of threads operating off a shared unbounded queue. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.