public abstract class AbstractThreadPoolProvider<E extends ThreadPoolExecutor> extends Object implements AutoCloseable
This class provides a skeleton implementation for provisioning and basic lifecycle management of thread pool executors.
Every instance of the concrete implementation of this provider class creates at most one shared and lazily initialized
thread pool executor instance, which can be retrieved by invoking the getExecutor()
method. This provider also makes
sure that the provisioned thread pool executor instance is properly shut down when the managing provider instance is
closed
(in case it has not been already shut down).
At minimum, concrete subclasses of this provider are expected to implement the createExecutor(int, java.util.concurrent.ThreadFactory, java.util.concurrent.RejectedExecutionHandler)
method that is used
as a thread pool instance factory. The method is invoked lazily, with the first call to the getExecutor()
method.
The result returned from the createExecutor()
method is cached internally and is used as a return value for subsequent
calls to the getExecutor()
method. This means, that createExecutor()
method is guaranteed to be invoked
at most once during the lifetime of any particular provider instance.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_TERMINATION_TIMEOUT
Default thread pool executor termination timeout in milliseconds.
|
Modifier | Constructor and Description |
---|---|
protected |
AbstractThreadPoolProvider(String name)
Inheritance constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close this thread pool executor provider.
|
protected abstract E |
createExecutor(int corePoolSize,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
Create a new instance of the thread pool executor that should be provided by the
getExecutor() method. |
protected ThreadFactory |
getBackingThreadFactory()
Get a backing thread factory that should be used as a delegate for creating the new threads for the provisioned executor
service.
|
protected int |
getCorePoolSize()
Get the number of the core threads of the the provisioned thread pool executor.
|
protected E |
getExecutor()
Get the thread pool executor.
|
protected RejectedExecutionHandler |
getRejectedExecutionHandler()
Get the handler for tasks that could not be executed by the provisioned thread pool executor.
|
protected int |
getTerminationTimeout()
Get the provisioned thread pool executor termination time out (in milliseconds).
|
boolean |
isClosed()
Check if this thread pool executor provider has been
closed . |
protected void |
onClose()
Close event handler, that invoked during the
close() operation. |
public static final int DEFAULT_TERMINATION_TIMEOUT
protected AbstractThreadPoolProvider(String name)
name
- name of the provided thread pool executor. Will be used in the names of threads created & used by the
provided thread pool executor.protected final E getExecutor()
createExecutor(int, java.util.concurrent.ThreadFactory, java.util.concurrent.RejectedExecutionHandler)
method to retrieve the
provided thread pool executor instance. The created thread pool executor instance is then cached and will be returned upon
subsequent calls to this method.IllegalStateException
- in case the provider has been closed
already.createExecutor(int, java.util.concurrent.ThreadFactory, java.util.concurrent.RejectedExecutionHandler)
,
close()
,
isClosed()
protected abstract E createExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler)
getExecutor()
method.
Concrete implementations of this class must override this method and implement the logic that creates the executor
service to be provided. The returned thread pool executor will be shut down when this provider instance is
closed
.
This method is invoked at most once, during the first call to the getExecutor()
method.
corePoolSize
- number of core threads the provisioned thread pool executor should provide.threadFactory
- thread factory to be used by the provisioned thread pool executor when creating new threads.handler
- handler for tasks that cannot be executed by the provisioned thread pool executor (e.g. due to a
shutdown).getExecutor()
,
close()
,
getCorePoolSize()
,
getBackingThreadFactory()
,
getRejectedExecutionHandler()
protected int getTerminationTimeout()
The method is used during the thread pool executor shutdown sequence to determine the shutdown timeout, when this provider
instance is closed
.
In case the thread pool executor shutdown is interrupted or the timeout expires, the provisioned thread pool executor is
shutdown forcefully
.
The method can be overridden to customize the thread pool executor termination time out. If not customized, the method defaults to 5000 ms.
close()
,
ExecutorService.awaitTermination(long, java.util.concurrent.TimeUnit)
protected int getCorePoolSize()
The value from this method is passed as one of the input parameters in a call to the createExecutor(int, java.util.concurrent.ThreadFactory, java.util.concurrent.RejectedExecutionHandler)
method.
The method can be overridden to customize the number of core threads of the provisioned thread pool executor.
If not customized, the method defaults to the number of available processors
in the system.
createExecutor(int, java.util.concurrent.ThreadFactory, java.util.concurrent.RejectedExecutionHandler)
protected RejectedExecutionHandler getRejectedExecutionHandler()
The value from this method is passed as one of the input parameters in a call to the createExecutor(int, java.util.concurrent.ThreadFactory, java.util.concurrent.RejectedExecutionHandler)
method.
The method can be overridden to customize the rejected task handler used by the provisioned thread pool executor. If not customized, the method provides a basic default NO-OP implementation.
createExecutor(int, java.util.concurrent.ThreadFactory, java.util.concurrent.RejectedExecutionHandler)
protected ThreadFactory getBackingThreadFactory()
The value from this method is used as a backing ThreadFactory
for an internally constructed thread factory
instance
that is passed as one of the input parameters in a call to the createExecutor(int, java.util.concurrent.ThreadFactory, java.util.concurrent.RejectedExecutionHandler)
method.
When not null
, the new threads will be created by invoking the ThreadFactory.newThread(Runnable)
on
this backing ThreadFactory
.
The method can be overridden to customize the backing thread factory for the provisioned thread pool executor.
If not customized, the method returns null
by default.
null
, in which case no backing
thread factory will be used.createExecutor(int, java.util.concurrent.ThreadFactory, java.util.concurrent.RejectedExecutionHandler)
public final boolean isClosed()
closed
.true
if this provider has been closed, false
otherwise.close()
protected void onClose()
close()
operation.
Concrete implementations of this provider class may override this method to perform any additional resource clean-up. Default implementation is a NO-OP.
close()
public final void close()
Once the provider is closed, it will stop providing the thread pool executor and subsequent invocations to
getExecutor()
method will result in an IllegalStateException
being thrown. The current
status of the provider can be checked via isClosed()
method.
Upon invocation, the following tasks are performed:
getExecutor()
method is stopped.onClose()
event handler is invoked.created
and provisioned
,
is shut down.
First, a graceful shutdown
is attempted. The value returned from
a call to getTerminationTimeout()
method is used to determine the graceful shutdown timeout period.
In case the thread pool executor graceful shutdown is interrupted or the timeout expires, the provisioned thread pool
executor is
shutdown forcefully
. All tasks that have never commenced
execution are then cancelled interruptingly
, if possible.
close
in interface AutoCloseable
isClosed()
,
onClose()
,
getExecutor()
,
getTerminationTimeout()
Copyright © 2007-2023, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.