Java.util.concurrent Executors: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(12 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executor.html
* https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html
* https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html
=Internal=
=Internal=


* [[java.util.concurrent#Subjects|java.util.concurrent]]
* [[java.util.concurrent#Executors|java.util.concurrent]]


=Overview=
=Overview=
Line 7: Line 13:
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 <tt>Thread</tt> instances.  The executor decouples the task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc.
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 <tt>Thread</tt> instances.  The executor decouples the task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc.


<font color=red>Difference between Executor and ExecutorService.</font>
The essential interface is <tt>java.util.concurrent.Executor</tt>, which defines an object that executes submitted Runnable or [[Java.util.concurrent#Callable|Callable]] 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.
 
Once it submitted a task for execution, the caller can be notified of task execution, asynchronously, via a <span id="future"></span>[[Java.util.concurrent#Future|Future]].
 
<tt>java.util.concurrent.ExecutorService</tt> extends <tt>java.util.concurrent.Executor</tt> 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. Upon creation, an ExecutorService is started and ready to accept tasks by default.


The package offers an executor instance factory class <tt>ava.util.concurrent.Executors</tt>.
The package offers an executor instance factory class <tt>java.util.concurrent.Executors</tt>, which is in general recommended to be used when a new executor is needed.


=Typical Usage=
=Typical Usage=


<pre>
<syntaxhighlight lang='java'>
int threadCount = 10;
int threadCount = 10;
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
ExecutorService executor = Executors.newFixedThreadPool(threadCount);


</pre>
...
 
executor.execute(() -> { ... });
 
</syntaxhighlight>


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.
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.

Latest revision as of 23:11, 26 October 2018

External

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 or Callable 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.

Once it submitted a task for execution, the caller can be notified of task execution, asynchronously, via a Future.

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. Upon creation, an ExecutorService is started and ready to accept tasks by default.

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(() -> { ... });

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.