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:
Executor,ExecutorService
- Direct Known Subclasses:
FairOrderedDownstreamThreadPoolExecutor
This is a fair alternative of Unfair of
The task executed in
OrderedMemoryAwareThreadPoolExecutor .
Unfair of OrderedMemoryAwareThreadPoolExecutor
The task executed in 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 ClassesModifier and TypeClassDescriptionprotected final classNested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final FairOrderedMemoryAwareThreadPoolExecutor.EventTaskprivate final AtomicReferenceFieldUpdater<FairOrderedMemoryAwareThreadPoolExecutor.EventTask, FairOrderedMemoryAwareThreadPoolExecutor.EventTask> protected final ConcurrentMap<Object, FairOrderedMemoryAwareThreadPoolExecutor.EventTask> -
Constructor Summary
ConstructorsConstructorDescriptionFairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize) Creates a new instance.FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, TimeUnit unit) Creates a new instance.FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, TimeUnit unit, ThreadFactory threadFactory) Creates a new instance.FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, TimeUnit unit, ObjectSizeEstimator objectSizeEstimator, ThreadFactory threadFactory) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionprotected final booleancompareAndSetNext(FairOrderedMemoryAwareThreadPoolExecutor.EventTask eventTask, FairOrderedMemoryAwareThreadPoolExecutor.EventTask expect, FairOrderedMemoryAwareThreadPoolExecutor.EventTask update) protected voidExecutes the specified task concurrently while maintaining the event order.protected ObjectnewMap()private voidremoveIfClosed(ChannelEventRunnable eventRunnable, Object key) protected booleancall removeKey(Object key) when the life cycle of the key ends, such as when the channel is closedprotected booleanshouldCount(Runnable task) Returnstrueif and only if the specifiedtaskshould 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, terminatedMethods 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, toStringMethods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submit
-
Field Details
-
end
-
fieldUpdater
-
map
-
-
Constructor Details
-
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. Specify0to disable.maxTotalMemorySize- the maximum total size of the queued events for this pool Specify0to disable.
-
FairOrderedMemoryAwareThreadPoolExecutor
public FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, TimeUnit unit) Creates a new instance.- Parameters:
corePoolSize- the maximum number of active threadsmaxChannelMemorySize- the maximum total size of the queued events per channel. Specify0to disable.maxTotalMemorySize- the maximum total size of the queued events for this pool Specify0to disable.keepAliveTime- the amount of time for an inactive thread to shut itself downunit- theTimeUnitofkeepAliveTime
-
FairOrderedMemoryAwareThreadPoolExecutor
public FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, TimeUnit unit, ThreadFactory threadFactory) Creates a new instance.- Parameters:
corePoolSize- the maximum number of active threadsmaxChannelMemorySize- the maximum total size of the queued events per channel. Specify0to disable.maxTotalMemorySize- the maximum total size of the queued events for this pool Specify0to disable.keepAliveTime- the amount of time for an inactive thread to shut itself downunit- theTimeUnitofkeepAliveTimethreadFactory- theThreadFactoryof this pool
-
FairOrderedMemoryAwareThreadPoolExecutor
public FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, TimeUnit unit, ObjectSizeEstimator objectSizeEstimator, ThreadFactory threadFactory) Creates a new instance.- Parameters:
corePoolSize- the maximum number of active threadsmaxChannelMemorySize- the maximum total size of the queued events per channel. Specify0to disable.maxTotalMemorySize- the maximum total size of the queued events for this pool Specify0to disable.keepAliveTime- the amount of time for an inactive thread to shut itself downunit- theTimeUnitofkeepAliveTimeobjectSizeEstimator- theObjectSizeEstimatorof this poolthreadFactory- theThreadFactoryof this pool
-
-
Method Details
-
newMap
-
doExecute
Executes the specified task concurrently while maintaining the event order.- Overrides:
doExecutein classMemoryAwareThreadPoolExecutor
-
removeIfClosed
-
removeKey
call removeKey(Object key) when the life cycle of the key ends, such as when the channel is closed -
getKey
-
shouldCount
Description copied from class:MemoryAwareThreadPoolExecutorReturnstrueif and only if the specifiedtaskshould 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:
shouldCountin classMemoryAwareThreadPoolExecutor
-
compareAndSetNext
protected final boolean compareAndSetNext(FairOrderedMemoryAwareThreadPoolExecutor.EventTask eventTask, FairOrderedMemoryAwareThreadPoolExecutor.EventTask expect, FairOrderedMemoryAwareThreadPoolExecutor.EventTask update)
-