public class FairOrderedMemoryAwareThreadPoolExecutor extends MemoryAwareThreadPoolExecutor
OrderedMemoryAwareThreadPoolExecutor
.
OrderedMemoryAwareThreadPoolExecutor
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".
----------------------------------------> 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.
Modifier and Type | Class and Description |
---|---|
protected class |
FairOrderedMemoryAwareThreadPoolExecutor.EventTask |
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy
Modifier and Type | Field and Description |
---|---|
protected ConcurrentMap<Object,FairOrderedMemoryAwareThreadPoolExecutor.EventTask> |
map |
Constructor and Description |
---|
FairOrderedMemoryAwareThreadPoolExecutor(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,
ObjectSizeEstimator objectSizeEstimator,
ThreadFactory threadFactory)
Creates a new instance.
|
FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize,
long maxChannelMemorySize,
long maxTotalMemorySize,
long keepAliveTime,
TimeUnit unit,
ThreadFactory threadFactory)
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
protected boolean |
compareAndSetNext(FairOrderedMemoryAwareThreadPoolExecutor.EventTask eventTask,
FairOrderedMemoryAwareThreadPoolExecutor.EventTask expect,
FairOrderedMemoryAwareThreadPoolExecutor.EventTask update) |
protected void |
doExecute(Runnable task)
Executes the specified task concurrently while maintaining the event order.
|
protected Object |
getKey(ChannelEvent e) |
protected ConcurrentMap<Object,FairOrderedMemoryAwareThreadPoolExecutor.EventTask> |
newMap() |
protected boolean |
removeKey(Object key)
call removeKey(Object key) when the life cycle of the key ends, such as when the channel is closed
|
protected boolean |
shouldCount(Runnable task)
Returns
true if and only if the specified task should
be counted to limit the global and per-channel memory consumption. |
beforeExecute, decreaseCounter, doUnorderedExecute, execute, getMaxChannelMemorySize, getMaxTotalMemorySize, getNotifyChannelFuturesOnShutdown, getObjectSizeEstimator, increaseCounter, remove, setMaxChannelMemorySize, setNotifyChannelFuturesOnShutdown, setObjectSizeEstimator, shutdownNow, shutdownNow, terminated
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
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submit
protected final ConcurrentMap<Object,FairOrderedMemoryAwareThreadPoolExecutor.EventTask> map
public FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize)
corePoolSize
- the maximum number of active threadsmaxChannelMemorySize
- the maximum total size of the queued events per channel. Specify 0
to
disable.maxTotalMemorySize
- the maximum total size of the queued events for this pool Specify 0
to
disable.public FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, TimeUnit unit)
corePoolSize
- the maximum number of active threadsmaxChannelMemorySize
- the maximum total size of the queued events per channel. Specify 0
to
disable.maxTotalMemorySize
- the maximum total size of the queued events for this pool Specify 0
to
disable.keepAliveTime
- the amount of time for an inactive thread to shut itself downunit
- the TimeUnit
of keepAliveTime
public FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, TimeUnit unit, ThreadFactory threadFactory)
corePoolSize
- the maximum number of active threadsmaxChannelMemorySize
- the maximum total size of the queued events per channel. Specify 0
to
disable.maxTotalMemorySize
- the maximum total size of the queued events for this pool Specify 0
to
disable.keepAliveTime
- the amount of time for an inactive thread to shut itself downunit
- the TimeUnit
of keepAliveTime
threadFactory
- the ThreadFactory
of this poolpublic FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, TimeUnit unit, ObjectSizeEstimator objectSizeEstimator, ThreadFactory threadFactory)
corePoolSize
- the maximum number of active threadsmaxChannelMemorySize
- the maximum total size of the queued events per channel. Specify 0
to
disable.maxTotalMemorySize
- the maximum total size of the queued events for this pool Specify 0
to
disable.keepAliveTime
- the amount of time for an inactive thread to shut itself downunit
- the TimeUnit
of keepAliveTime
threadFactory
- the ThreadFactory
of this poolobjectSizeEstimator
- the ObjectSizeEstimator
of this poolprotected ConcurrentMap<Object,FairOrderedMemoryAwareThreadPoolExecutor.EventTask> newMap()
protected void doExecute(Runnable task)
doExecute
in class MemoryAwareThreadPoolExecutor
protected boolean removeKey(Object key)
protected Object getKey(ChannelEvent e)
protected boolean shouldCount(Runnable task)
MemoryAwareThreadPoolExecutor
true
if and only if the specified task
should
be counted to limit the global and per-channel memory consumption.
To override this method, you must call super.shouldCount()
to
make sure important tasks are not counted.shouldCount
in class MemoryAwareThreadPoolExecutor
protected final boolean compareAndSetNext(FairOrderedMemoryAwareThreadPoolExecutor.EventTask eventTask, FairOrderedMemoryAwareThreadPoolExecutor.EventTask expect, FairOrderedMemoryAwareThreadPoolExecutor.EventTask update)
Copyright © 2008-2016 The Netty Project. All Rights Reserved.