Class FairOrderedMemoryAwareThreadPoolExecutor

All Implemented Interfaces:
Executor, ExecutorService
Direct Known Subclasses:
FairOrderedDownstreamThreadPoolExecutor

public class FairOrderedMemoryAwareThreadPoolExecutor extends MemoryAwareThreadPoolExecutor
This is a fair alternative of 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.
  • Field Details

  • Constructor Details

    • FairOrderedMemoryAwareThreadPoolExecutor

      public FairOrderedMemoryAwareThreadPoolExecutor(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.
    • FairOrderedMemoryAwareThreadPoolExecutor

      public FairOrderedMemoryAwareThreadPoolExecutor(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
    • 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 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
    • 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 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 Details