|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.util.concurrent.AbstractExecutorService
java.util.concurrent.ThreadPoolExecutor
org.jboss.netty.handler.execution.MemoryAwareThreadPoolExecutor
public class MemoryAwareThreadPoolExecutor
A ThreadPoolExecutor which blocks the task submission when there's
too many tasks in the queue. Both per-Channel and per-Executor
limitation can be applied.
When a task (i.e. Runnable) is submitted,
MemoryAwareThreadPoolExecutor calls ObjectSizeEstimator.estimateSize(Object)
to get the estimated size of the task in bytes to calculate the amount of
memory occupied by the unprocessed tasks.
If the total size of the unprocessed tasks exceeds either per-Channel
or per-Executor threshold, any further execute(Runnable)
call will block until the tasks in the queue are processed so that the total
size goes under the threshold.
ObjectSizeEstimator implementation instead of the
DefaultObjectSizeEstimator to avoid incorrect task size calculation,
especially when:
MemoryAwareThreadPoolExecutor independently from
ExecutionHandler,ChannelEventRunnable, orMessageEvent in the ChannelEventRunnable
is not ChannelBuffer.ObjectSizeEstimator
which understands a user-defined object:
public class MyRunnable implementsRunnable{ private final byte[] data; public MyRunnable(byte[] data) { this.data = data; } public void run() { // Process 'data' .. } } public class MyObjectSizeEstimator extendsDefaultObjectSizeEstimator{ @Override public int estimateSize(Object o) { if (o instanceof MyRunnable) { return ((MyRunnable) o).data.length + 8; } return super.estimateSize(o); } }ThreadPoolExecutorpool = newMemoryAwareThreadPoolExecutor( 16, 65536, 1048576, 30,TimeUnit.SECONDS, new MyObjectSizeEstimator(),Executors.defaultThreadFactory()); pool.execute(new MyRunnable(data));
ChannelEvents for the same Channel. For example,
you can even receive a "channelClosed" event before a
"messageReceived" event, as depicted by the following diagram.
For example, the events can be processed as depicted below:
--------------------------------> Timeline -------------------------------->
Thread X: --- Channel A (Event 2) --- Channel A (Event 1) --------------------------->
Thread Y: --- Channel A (Event 3) --- Channel B (Event 2) --- Channel B (Event 3) --->
Thread Z: --- Channel B (Event 1) --- Channel B (Event 4) --- Channel A (Event 4) --->
To maintain the event order, you must use OrderedMemoryAwareThreadPoolExecutor.
| Nested Class Summary |
|---|
| 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 |
| Constructor Summary | |
|---|---|
MemoryAwareThreadPoolExecutor(int corePoolSize,
long maxChannelMemorySize,
long maxTotalMemorySize)
Creates a new instance. |
|
MemoryAwareThreadPoolExecutor(int corePoolSize,
long maxChannelMemorySize,
long maxTotalMemorySize,
long keepAliveTime,
java.util.concurrent.TimeUnit unit)
Creates a new instance. |
|
MemoryAwareThreadPoolExecutor(int corePoolSize,
long maxChannelMemorySize,
long maxTotalMemorySize,
long keepAliveTime,
java.util.concurrent.TimeUnit unit,
ObjectSizeEstimator objectSizeEstimator,
java.util.concurrent.ThreadFactory threadFactory)
Creates a new instance. |
|
MemoryAwareThreadPoolExecutor(int corePoolSize,
long maxChannelMemorySize,
long maxTotalMemorySize,
long keepAliveTime,
java.util.concurrent.TimeUnit unit,
java.util.concurrent.ThreadFactory threadFactory)
Creates a new instance. |
|
| Method Summary | |
|---|---|
protected void |
beforeExecute(java.lang.Thread t,
java.lang.Runnable r)
|
protected void |
decreaseCounter(java.lang.Runnable task)
|
protected void |
doExecute(java.lang.Runnable task)
Put the actual execution logic here. |
protected void |
doUnorderedExecute(java.lang.Runnable task)
Executes the specified task without maintaining the event order. |
void |
execute(java.lang.Runnable command)
|
long |
getMaxChannelMemorySize()
Returns the maximum total size of the queued events per channel. |
long |
getMaxTotalMemorySize()
Returns the maximum total size of the queued events for this pool. |
ObjectSizeEstimator |
getObjectSizeEstimator()
Returns the ObjectSizeEstimator of this pool. |
protected boolean |
increaseCounter(java.lang.Runnable task)
|
boolean |
remove(java.lang.Runnable task)
|
void |
setMaxChannelMemorySize(long maxChannelMemorySize)
Sets the maximum total size of the queued events per channel. |
void |
setMaxTotalMemorySize(long maxTotalMemorySize)
Sets the maximum total size of the queued events for this pool. |
void |
setObjectSizeEstimator(ObjectSizeEstimator objectSizeEstimator)
Sets the ObjectSizeEstimator of this pool. |
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. |
protected void |
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, shutdownNow |
| Methods inherited from class java.util.concurrent.AbstractExecutorService |
|---|
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submit |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public MemoryAwareThreadPoolExecutor(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 MemoryAwareThreadPoolExecutor(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 keepAliveTime
public MemoryAwareThreadPoolExecutor(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 pool
public MemoryAwareThreadPoolExecutor(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 pool| Method Detail |
|---|
protected void terminated()
terminated in class java.util.concurrent.ThreadPoolExecutorpublic ObjectSizeEstimator getObjectSizeEstimator()
ObjectSizeEstimator of this pool.
public void setObjectSizeEstimator(ObjectSizeEstimator objectSizeEstimator)
ObjectSizeEstimator of this pool.
public long getMaxChannelMemorySize()
public void setMaxChannelMemorySize(long maxChannelMemorySize)
0 to disable.
public long getMaxTotalMemorySize()
public void setMaxTotalMemorySize(long maxTotalMemorySize)
0 to disable.
public void execute(java.lang.Runnable command)
execute in interface java.util.concurrent.Executorexecute in class java.util.concurrent.ThreadPoolExecutorprotected void doExecute(java.lang.Runnable task)
doUnorderedExecute(Runnable).
protected final void doUnorderedExecute(java.lang.Runnable task)
public boolean remove(java.lang.Runnable task)
remove in class java.util.concurrent.ThreadPoolExecutor
protected void beforeExecute(java.lang.Thread t,
java.lang.Runnable r)
beforeExecute in class java.util.concurrent.ThreadPoolExecutorprotected boolean increaseCounter(java.lang.Runnable task)
protected void decreaseCounter(java.lang.Runnable task)
protected boolean shouldCount(java.lang.Runnable task)
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.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||