public class FairOrderedMemoryAwareThreadPoolExecutor extends MemoryAwareThreadPoolExecutor
OrderedMemoryAwareThreadPoolExecutor .
OrderedMemoryAwareThreadPoolExecutorOrderedMemoryAwareThreadPoolExecutor 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.| 限定符和类型 | 类和说明 |
|---|---|
protected class |
FairOrderedMemoryAwareThreadPoolExecutor.EventTask |
| 限定符和类型 | 字段和说明 |
|---|---|
protected java.util.concurrent.ConcurrentMap<java.lang.Object,FairOrderedMemoryAwareThreadPoolExecutor.EventTask> |
map |
| 构造器和说明 |
|---|
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,
ObjectSizeEstimator objectSizeEstimator,
java.util.concurrent.ThreadFactory threadFactory)
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.
|
| 限定符和类型 | 方法和说明 |
|---|---|
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() |
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
|
protected boolean |
shouldCount(java.lang.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, terminatedafterExecute, 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, toStringprotected final java.util.concurrent.ConcurrentMap<java.lang.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,
java.util.concurrent.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 keepAliveTimepublic FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize,
long maxChannelMemorySize,
long maxTotalMemorySize,
long keepAliveTime,
java.util.concurrent.TimeUnit unit,
java.util.concurrent.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 keepAliveTimethreadFactory - the ThreadFactory of this poolpublic FairOrderedMemoryAwareThreadPoolExecutor(int corePoolSize,
long maxChannelMemorySize,
long maxTotalMemorySize,
long keepAliveTime,
java.util.concurrent.TimeUnit unit,
ObjectSizeEstimator objectSizeEstimator,
java.util.concurrent.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 keepAliveTimethreadFactory - the ThreadFactory of this poolobjectSizeEstimator - the ObjectSizeEstimator of this poolprotected java.util.concurrent.ConcurrentMap<java.lang.Object,FairOrderedMemoryAwareThreadPoolExecutor.EventTask> newMap()
protected void doExecute(java.lang.Runnable task)
protected boolean removeKey(java.lang.Object key)
protected java.lang.Object getKey(ChannelEvent e)
protected boolean shouldCount(java.lang.Runnable task)
MemoryAwareThreadPoolExecutortrue 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.protected final boolean compareAndSetNext(FairOrderedMemoryAwareThreadPoolExecutor.EventTask eventTask, FairOrderedMemoryAwareThreadPoolExecutor.EventTask expect, FairOrderedMemoryAwareThreadPoolExecutor.EventTask update)