org.jboss.netty.handler.execution
Class MemoryAwareThreadPoolExecutor

java.lang.Object
  extended by java.util.concurrent.AbstractExecutorService
      extended by java.util.concurrent.ThreadPoolExecutor
          extended by org.jboss.netty.handler.execution.MemoryAwareThreadPoolExecutor
All Implemented Interfaces:
Executor, ExecutorService
Direct Known Subclasses:
OrderedMemoryAwareThreadPoolExecutor

public class MemoryAwareThreadPoolExecutor
extends ThreadPoolExecutor

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. If the total size of the unprocessed tasks (i.e. Runnables) 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 is used to calculate the size of each task.

Please note that this executor does not maintain the order of the 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.

Version:
$Rev: 1685 $, $Date: 2009-08-28 16:15:49 +0900 (금, 28 8 2009) $
Author:
The Netty Project (netty-dev@lists.jboss.org), Trustin Lee (tlee@redhat.com)

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy
 
Constructor Summary
MemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize)
          Creates a new instance.
MemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, TimeUnit unit)
          Creates a new instance.
MemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, TimeUnit unit, ObjectSizeEstimator objectSizeEstimator, ThreadFactory threadFactory)
          Creates a new instance.
MemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, TimeUnit unit, ThreadFactory threadFactory)
          Creates a new instance.
 
Method Summary
protected  void beforeExecute(Thread t, Runnable r)
           
protected  void decreaseCounter(Runnable task)
           
protected  void doExecute(Runnable task)
          Put the actual execution logic here.
protected  void doUnorderedExecute(Runnable task)
          Executes the specified task without maintaining the event order.
 void execute(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(Runnable task)
           
 boolean remove(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(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

MemoryAwareThreadPoolExecutor

public MemoryAwareThreadPoolExecutor(int corePoolSize,
                                     long maxChannelMemorySize,
                                     long maxTotalMemorySize)
Creates a new instance.

Parameters:
corePoolSize - the maximum number of active threads
maxChannelMemorySize - 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.

MemoryAwareThreadPoolExecutor

public MemoryAwareThreadPoolExecutor(int corePoolSize,
                                     long maxChannelMemorySize,
                                     long maxTotalMemorySize,
                                     long keepAliveTime,
                                     TimeUnit unit)
Creates a new instance.

Parameters:
corePoolSize - the maximum number of active threads
maxChannelMemorySize - 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 down
unit - the TimeUnit of keepAliveTime

MemoryAwareThreadPoolExecutor

public MemoryAwareThreadPoolExecutor(int corePoolSize,
                                     long maxChannelMemorySize,
                                     long maxTotalMemorySize,
                                     long keepAliveTime,
                                     TimeUnit unit,
                                     ThreadFactory threadFactory)
Creates a new instance.

Parameters:
corePoolSize - the maximum number of active threads
maxChannelMemorySize - 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 down
unit - the TimeUnit of keepAliveTime
threadFactory - the ThreadFactory of this pool

MemoryAwareThreadPoolExecutor

public MemoryAwareThreadPoolExecutor(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 threads
maxChannelMemorySize - 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 down
unit - the TimeUnit of keepAliveTime
threadFactory - the ThreadFactory of this pool
objectSizeEstimator - the ObjectSizeEstimator of this pool
Method Detail

terminated

protected void terminated()
Overrides:
terminated in class ThreadPoolExecutor

getObjectSizeEstimator

public ObjectSizeEstimator getObjectSizeEstimator()
Returns the ObjectSizeEstimator of this pool.


setObjectSizeEstimator

public void setObjectSizeEstimator(ObjectSizeEstimator objectSizeEstimator)
Sets the ObjectSizeEstimator of this pool.


getMaxChannelMemorySize

public long getMaxChannelMemorySize()
Returns the maximum total size of the queued events per channel.


setMaxChannelMemorySize

public void setMaxChannelMemorySize(long maxChannelMemorySize)
Sets the maximum total size of the queued events per channel. Specify 0 to disable.


getMaxTotalMemorySize

public long getMaxTotalMemorySize()
Returns the maximum total size of the queued events for this pool.


setMaxTotalMemorySize

public void setMaxTotalMemorySize(long maxTotalMemorySize)
Sets the maximum total size of the queued events for this pool. Specify 0 to disable.


execute

public void execute(Runnable command)
Specified by:
execute in interface Executor
Overrides:
execute in class ThreadPoolExecutor

doExecute

protected void doExecute(Runnable task)
Put the actual execution logic here. The default implementation simply calls doUnorderedExecute(Runnable).


doUnorderedExecute

protected final void doUnorderedExecute(Runnable task)
Executes the specified task without maintaining the event order.


remove

public boolean remove(Runnable task)
Overrides:
remove in class ThreadPoolExecutor

beforeExecute

protected void beforeExecute(Thread t,
                             Runnable r)
Overrides:
beforeExecute in class ThreadPoolExecutor

increaseCounter

protected boolean increaseCounter(Runnable task)

decreaseCounter

protected void decreaseCounter(Runnable task)

shouldCount

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. To override this method, you must call super.shouldCount() to make sure important tasks are not counted.



Copyright © 2008-2009 JBoss, by Red Hat. All Rights Reserved.