- All Implemented Interfaces:
AutoCloseable
Also allows for delayed execution of tasks using an internal Timer.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes this executor service instance.An executor service that will execute at most two tasks per processor on multi-CPU systems or one task on single-CPU systems.int
Gets the preferred concurrency for this executor.A sequential implementation of executor that performs no concurrent processing.Gets the unbounded executor.static Executors
Deprecated.Just use constructor directly.toString()
protected Runnable
Wraps the task with any other necessary tasks to prepare or cleanup for the task.protected <T> Callable
<T> Wraps the task with any other necessary tasks to prepare or cleanup for the task.
-
Constructor Details
-
Executors
public Executors()Create a new instance of the executor service.close()
must be called when done with the instance. This should be done in a try-with-resources, try-finally, or strong equivalent, such asServlet.destroy()
.Internally, threads are shared between executor instances. The threads are only shutdown when the last executor is closed.
- See Also:
-
-
Method Details
-
newInstance
Deprecated.Just use constructor directly. Subclasses are now allowed, too.Creates a new executors. -
toString
-
getPreferredConcurrency
public int getPreferredConcurrency()Gets the preferred concurrency for this executor. Does not change for the life of the executor, but will be updated should the last executor be closed and another created.This will always be
1
on a single-CPU system, not a multiple of CPU count. -
wrap
Wraps the task with any other necessary tasks to prepare or cleanup for the task.- See Also:
-
wrap
Wraps the task with any other necessary tasks to prepare or cleanup for the task.- See Also:
-
getUnbounded
Gets the unbounded executor. This is most appropriate for I/O bound tasks, especially higher latency I/O like wide area networks. -
getPerProcessor
An executor service that will execute at most two tasks per processor on multi-CPU systems or one task on single-CPU systems.This should be used for CPU-bound tasks that generally operate non-blocking. If a thread blocks or deadlocks, it can starve the system entirely - use this cautiously. For example, do not use it for things like disk I/O or network I/O (including writes - they can block, too, once the network buffers are full).
When a task is submitted by a thread that is already part of a per-processor executor, it will be invoked on a different per-processor executor to avoid potential deadlock. This means the total number of threads can exceed two tasks per processor, but it will remain bounded as a function of:
maxThreads = maxPerProcessorDepth * numProcessors * 2
Where maxPerProcessorDepth is a function of the number of times a per-processor task adds a per-processor task of its own.
- See Also:
-
getSequential
A sequential implementation of executor that performs no concurrent processing. All tasks are performed on the thread callingFuture.get()
.Important: A task submitted to this sequential executor is only processed when
Future.get()
is called. Thus, a task submitted without a call toFuture.get()
is never executed. This is in stark contrast to other executors, which will complete the task regardless.Note: Timeout not implemented
-
close
public void close()Closes this executor service instance. Once closed, no additional tasks may be submitted. Any overriding method must call super.close().If this is the last active executor, the underlying threads will also be shutdown. This shutdown may wait up to
(1 + numPerProcessorPools) * CLOSE_WAIT_NANOS
for clean termination of all threads.If already closed, no action will be taken and no exception thrown.
- Specified by:
close
in interfaceAutoCloseable
- See Also:
-