Class FairOrderedMemoryAwareThreadPoolExecutor
- java.lang.Object
-
- java.util.concurrent.AbstractExecutorService
-
- java.util.concurrent.ThreadPoolExecutor
-
- org.jboss.netty.handler.execution.MemoryAwareThreadPoolExecutor
-
- org.jboss.netty.handler.execution.FairOrderedMemoryAwareThreadPoolExecutor
-
- All Implemented Interfaces:
java.util.concurrent.Executor
,java.util.concurrent.ExecutorService
- Direct Known Subclasses:
FairOrderedDownstreamThreadPoolExecutor
public class FairOrderedMemoryAwareThreadPoolExecutor extends MemoryAwareThreadPoolExecutor
This is a fair alternative ofOrderedMemoryAwareThreadPoolExecutor
.Unfair of
The task executed inOrderedMemoryAwareThreadPoolExecutor
OrderedMemoryAwareThreadPoolExecutor
is unfair in some situations. For example, let's say there is only one executor thread that handle the events from the two channels, and events are submitted in sequence:Channel A (Event A1) , Channel B (Event B), Channel A (Event A2) , ... , Channel A (Event An)
Then the events maybe executed in this unfair order:----------------------------------------> Timeline --------------------------------> Channel A (Event A1) , Channel A (Event A2) , ... , Channel A (Event An), Channel B (Event B)
As we see above, Channel B (Event B) maybe executed unfairly late. Even more, if there are too much events come in Channel A, and one-by-one closely, then Channel B (Event B) would be waiting for a long while and become "hungry".Fair of FairOrderedMemoryAwareThreadPoolExecutor
In the same case above ( one executor thread and two channels ) , this implement will guarantee execution order as:----------------------------------------> Timeline --------------------------------> Channel A (Event A1) , Channel B (Event B), Channel A (Event A2) , ... , Channel A (Event An),
NOTE: For convenience the case above use one single executor thread, but the fair mechanism is suitable for multiple executor threads situations.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
FairOrderedMemoryAwareThreadPoolExecutor.EventTask
-
Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor
java.util.concurrent.ThreadPoolExecutor.AbortPolicy, java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy, java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy, java.util.concurrent.ThreadPoolExecutor.DiscardPolicy
-
-
Field Summary
Fields Modifier and Type Field Description private FairOrderedMemoryAwareThreadPoolExecutor.EventTask
end
private java.util.concurrent.atomic.AtomicReferenceFieldUpdater<FairOrderedMemoryAwareThreadPoolExecutor.EventTask,FairOrderedMemoryAwareThreadPoolExecutor.EventTask>
fieldUpdater
protected java.util.concurrent.ConcurrentMap<java.lang.Object,FairOrderedMemoryAwareThreadPoolExecutor.EventTask>
map
-
Constructor Summary
Constructors Constructor Description FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize)
Creates a new instance.FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, java.util.concurrent.TimeUnit unit)
Creates a new instance.FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.ThreadFactory threadFactory)
Creates a new instance.FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, java.util.concurrent.TimeUnit unit, ObjectSizeEstimator objectSizeEstimator, java.util.concurrent.ThreadFactory threadFactory)
Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected boolean
compareAndSetNext(FairOrderedMemoryAwareThreadPoolExecutor.EventTask eventTask, FairOrderedMemoryAwareThreadPoolExecutor.EventTask expect, FairOrderedMemoryAwareThreadPoolExecutor.EventTask update)
protected void
doExecute(java.lang.Runnable task)
Executes the specified task concurrently while maintaining the event order.protected java.lang.Object
getKey(ChannelEvent e)
protected java.util.concurrent.ConcurrentMap<java.lang.Object,FairOrderedMemoryAwareThreadPoolExecutor.EventTask>
newMap()
private void
removeIfClosed(ChannelEventRunnable eventRunnable, java.lang.Object key)
protected boolean
removeKey(java.lang.Object key)
call removeKey(Object key) when the life cycle of the key ends, such as when the channel is closedprotected boolean
shouldCount(java.lang.Runnable task)
Returnstrue
if and only if the specifiedtask
should be counted to limit the global and per-channel memory consumption.-
Methods inherited from class org.jboss.netty.handler.execution.MemoryAwareThreadPoolExecutor
beforeExecute, decreaseCounter, doUnorderedExecute, execute, getMaxChannelMemorySize, getMaxTotalMemorySize, getNotifyChannelFuturesOnShutdown, getObjectSizeEstimator, increaseCounter, remove, setMaxChannelMemorySize, setNotifyChannelFuturesOnShutdown, setObjectSizeEstimator, shutdownNow, shutdownNow, terminated
-
Methods inherited from class java.util.concurrent.ThreadPoolExecutor
afterExecute, allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getQueue, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, setThreadFactory, shutdown, toString
-
-
-
-
Field Detail
-
end
private final FairOrderedMemoryAwareThreadPoolExecutor.EventTask end
-
fieldUpdater
private final java.util.concurrent.atomic.AtomicReferenceFieldUpdater<FairOrderedMemoryAwareThreadPoolExecutor.EventTask,FairOrderedMemoryAwareThreadPoolExecutor.EventTask> fieldUpdater
-
map
protected final java.util.concurrent.ConcurrentMap<java.lang.Object,FairOrderedMemoryAwareThreadPoolExecutor.EventTask> map
-
-
Constructor Detail
-
FairOrderedMemoryAwareThreadPoolExecutor
public FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize)
Creates a new instance.- Parameters:
corePoolSize
- the maximum number of active threadsmaxChannelMemorySize
- the maximum total size of the queued events per channel. Specify0
to disable.maxTotalMemorySize
- the maximum total size of the queued events for this pool Specify0
to disable.
-
FairOrderedMemoryAwareThreadPoolExecutor
public FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, java.util.concurrent.TimeUnit unit)
Creates a new instance.- Parameters:
corePoolSize
- the maximum number of active threadsmaxChannelMemorySize
- the maximum total size of the queued events per channel. Specify0
to disable.maxTotalMemorySize
- the maximum total size of the queued events for this pool Specify0
to disable.keepAliveTime
- the amount of time for an inactive thread to shut itself downunit
- theTimeUnit
ofkeepAliveTime
-
FairOrderedMemoryAwareThreadPoolExecutor
public FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.ThreadFactory threadFactory)
Creates a new instance.- Parameters:
corePoolSize
- the maximum number of active threadsmaxChannelMemorySize
- the maximum total size of the queued events per channel. Specify0
to disable.maxTotalMemorySize
- the maximum total size of the queued events for this pool Specify0
to disable.keepAliveTime
- the amount of time for an inactive thread to shut itself downunit
- theTimeUnit
ofkeepAliveTime
threadFactory
- theThreadFactory
of this pool
-
FairOrderedMemoryAwareThreadPoolExecutor
public FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, java.util.concurrent.TimeUnit unit, ObjectSizeEstimator objectSizeEstimator, java.util.concurrent.ThreadFactory threadFactory)
Creates a new instance.- Parameters:
corePoolSize
- the maximum number of active threadsmaxChannelMemorySize
- the maximum total size of the queued events per channel. Specify0
to disable.maxTotalMemorySize
- the maximum total size of the queued events for this pool Specify0
to disable.keepAliveTime
- the amount of time for an inactive thread to shut itself downunit
- theTimeUnit
ofkeepAliveTime
threadFactory
- theThreadFactory
of this poolobjectSizeEstimator
- theObjectSizeEstimator
of this pool
-
-
Method Detail
-
newMap
protected java.util.concurrent.ConcurrentMap<java.lang.Object,FairOrderedMemoryAwareThreadPoolExecutor.EventTask> newMap()
-
doExecute
protected void doExecute(java.lang.Runnable task)
Executes the specified task concurrently while maintaining the event order.- Overrides:
doExecute
in classMemoryAwareThreadPoolExecutor
-
removeIfClosed
private void removeIfClosed(ChannelEventRunnable eventRunnable, java.lang.Object key)
-
removeKey
protected boolean removeKey(java.lang.Object key)
call removeKey(Object key) when the life cycle of the key ends, such as when the channel is closed
-
getKey
protected java.lang.Object getKey(ChannelEvent e)
-
shouldCount
protected boolean shouldCount(java.lang.Runnable task)
Description copied from class:MemoryAwareThreadPoolExecutor
Returnstrue
if and only if the specifiedtask
should be counted to limit the global and per-channel memory consumption. To override this method, you must callsuper.shouldCount()
to make sure important tasks are not counted.- Overrides:
shouldCount
in classMemoryAwareThreadPoolExecutor
-
compareAndSetNext
protected final boolean compareAndSetNext(FairOrderedMemoryAwareThreadPoolExecutor.EventTask eventTask, FairOrderedMemoryAwareThreadPoolExecutor.EventTask expect, FairOrderedMemoryAwareThreadPoolExecutor.EventTask update)
-
-