public class ReferenceCountingExecutorService extends Object
It is important to ensure that an executor service is correctly shut down as failing to do so prevents the JVM from terminating due to the existence of non-daemon threads.
TODO Might be more elegant to make this actually implement ExecutorService, providing better hiding of the implementation details. Also this class introduces a pattern (albeit specific to this usage) that could be generalized to reference count anything. That is, on first instance call a create method, on release of last instance call a destroy method. This could definitely be abstracted out as a re-usable piece of code; a reference counting factory. It could then be re-used to do reference counting in other places (such as messages). Countable objects have a simple create/destroy life cycle, capturable by an interface that the ref counting factory can call to manage the lifecycle.
TODO The getPool() method breaks the encapsulation of the reference counter. Generally when getPool is used
further checks are applied to ensure that the executor service has not been shutdown. This passes responsibility
for managing the lifecycle of the reference counted object onto the caller rather than neatly encapsulating it
here. Could think about adding more state to the lifecycle, to mark ref counted objects as invalid, and have an
isValid method, or could make calling code deal with RejectedExecutionException raised by shutdown executors.
| Modifier and Type | Method and Description |
|---|---|
ExecutorService |
acquireExecutorService()
Provides a reference to a shared executor service, incrementing the reference count.
|
static ReferenceCountingExecutorService |
getInstance()
Retrieves the singleton instance of this reference counter.
|
ExecutorService |
getPool()
Provides access to the executor service, without touching the reference count.
|
int |
getReferenceCount()
Return the ReferenceCount to this ExecutorService
|
ThreadFactory |
getThreadFactory()
Return the thread factory used by the
ThreadPoolExecutor to create new threads. |
void |
releaseExecutorService()
Releases a reference to a shared executor service, decrementing the reference count.
|
void |
setThreadFactory(ThreadFactory threadFactory)
Sets the thread factory used by the
ThreadPoolExecutor to create new threads. |
public static ReferenceCountingExecutorService getInstance()
public ExecutorService acquireExecutorService()
public void releaseExecutorService()
public ExecutorService getPool()
public int getReferenceCount()
public ThreadFactory getThreadFactory()
ThreadPoolExecutor to create new threads.public void setThreadFactory(ThreadFactory threadFactory)
ThreadPoolExecutor to create new threads.
If the pool has been already created, the change will have no effect until
getReferenceCount() reaches zero and the pool recreated. For this reason,
callers must invoke this method before calling acquireExecutorService().
threadFactory - thread factoryCopyright © 2006–2015 The Apache Software Foundation. All rights reserved.