Package org.jboss.netty.util
Class VirtualExecutorService
- java.lang.Object
-
- java.util.concurrent.AbstractExecutorService
-
- org.jboss.netty.util.VirtualExecutorService
-
- All Implemented Interfaces:
java.util.concurrent.Executor
,java.util.concurrent.ExecutorService
public class VirtualExecutorService extends java.util.concurrent.AbstractExecutorService
A delegatingExecutorService
with its own termination management.VirtualExecutorService
is used when you want to inject anExecutorService
but you do not want to allow the explicit termination of threads on shutdown request. It is particularly useful when theExecutorService
to inject is shared by different components and the life cycle of the components depend on the termination of the injectedExecutorService
.ExecutorService globalExecutor = ...; ExecutorService virtualExecutor = new
VirtualExecutorService
(globalExecutor);ChannelFactory
factory = newNioServerSocketChannelFactory
(virtualExecutor, virtualExecutor); ... // ChannelFactory.releaseExternalResources() shuts down the executor and // interrupts the I/O threads to terminate all I/O tasks and to release all // resources acquired by ChannelFactory. factory.releaseExternalResources(); // Note that globalExecutor is not shut down because VirtualExecutorService // implements its own termination management. All threads which were acquired // by ChannelFactory via VirtualExecutorService are returned to the pool. assert !globalExecutor.isShutdown();The differences from an ordinary
A shutdown request (ExecutorService
shutdown()
orshutdownNow()
) does not shut down its parentExecutor
but simply sets its internal flag to reject further execution request.shutdownNow()
interrupts only the thread which is executing the task executed viaVirtualExecutorService
.awaitTermination(long, TimeUnit)
does not wait for real thread termination but wait untilVirtualExecutorService
is shut down and its active tasks are finished and the threads are returned to the parentExecutor
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
VirtualExecutorService.ChildExecutorRunnable
-
Field Summary
Fields Modifier and Type Field Description (package private) java.util.Set<java.lang.Thread>
activeThreads
private java.util.concurrent.Executor
e
private java.util.concurrent.ExecutorService
s
(package private) boolean
shutdown
(package private) java.lang.Object
startStopLock
-
Constructor Summary
Constructors Constructor Description VirtualExecutorService(java.util.concurrent.Executor parent)
Creates a new instance with the specified parentExecutor
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
awaitTermination(long timeout, java.util.concurrent.TimeUnit unit)
void
execute(java.lang.Runnable command)
boolean
isShutdown()
boolean
isTerminated()
void
shutdown()
java.util.List<java.lang.Runnable>
shutdownNow()
-
-
-
Method Detail
-
isShutdown
public boolean isShutdown()
-
isTerminated
public boolean isTerminated()
-
shutdown
public void shutdown()
-
shutdownNow
public java.util.List<java.lang.Runnable> shutdownNow()
-
awaitTermination
public boolean awaitTermination(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException
-
execute
public void execute(java.lang.Runnable command)
-
-