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 class
Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final FairOrderedMemoryAwareThreadPoolExecutor.EventTask
private 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 boolean
compareAndSetNext
(FairOrderedMemoryAwareThreadPoolExecutor.EventTask eventTask, FairOrderedMemoryAwareThreadPoolExecutor.EventTask expect, FairOrderedMemoryAwareThreadPoolExecutor.EventTask update) protected void
Executes the specified task concurrently while maintaining the event order.protected Object
newMap()
private void
removeIfClosed
(ChannelEventRunnable eventRunnable, Object key) protected boolean
call removeKey(Object key) when the life cycle of the key ends, such as when the channel is closedprotected boolean
shouldCount
(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
Methods 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. 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, 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, 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. 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, 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. 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 Details
-
newMap
-
doExecute
Executes the specified task concurrently while maintaining the event order.- Overrides:
doExecute
in 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: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)
-