winstone
Class BoundedExecutorService

java.lang.Object
  extended by java.util.concurrent.AbstractExecutorService
      extended by winstone.BoundedExecutorService
All Implemented Interfaces:
java.util.concurrent.Executor, java.util.concurrent.ExecutorService

public class BoundedExecutorService
extends java.util.concurrent.AbstractExecutorService

Wraps Executor so that we only ask the wrapped Executor to execute N number of tasks at any given time.

The intention is to use this with ThreadPoolExecutor with SynchronousQueue with unbounded max capacity (so that for up to N tasks we keep creating more threads for work, but beyond that we start to push the tasks into the queue of an infinite capacity.)

This is necessary because ThreadPoolExecutor tries to push work into the queue first and only create more threads once the queue is full, so for a queue with infinite capacity it'll never create threads beyond the core pool size. See http://www.kimchy.org/juc-executorservice-gotcha/ for more discussion of this.

Because there's no call back to tell us when the wrapped ExecutorService has finished executing something, this class needs to hand out the next task slightly before the wrapped ExecutorService is done with the previous task. The net result is that the wrapped ExecutorService will end up running N+1 threads (of which 1 is almost always idle.) I'm not sure how to fix this.

Author:
Kohsuke Kawaguchi

Constructor Summary
BoundedExecutorService(java.util.concurrent.ExecutorService base, int max)
           
 
Method Summary
 boolean awaitTermination(long timeout, java.util.concurrent.TimeUnit unit)
           
 void execute(java.lang.Runnable r)
           
 boolean isShutdown()
           
 boolean isTerminated()
           
 void shutdown()
           
 java.util.List<java.lang.Runnable> 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, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BoundedExecutorService

public BoundedExecutorService(java.util.concurrent.ExecutorService base,
                              int max)
Method Detail

execute

public void execute(java.lang.Runnable r)

shutdown

public void shutdown()

shutdownNow

public java.util.List<java.lang.Runnable> shutdownNow()

isShutdown

public boolean isShutdown()

isTerminated

public boolean isTerminated()

awaitTermination

public boolean awaitTermination(long timeout,
                                java.util.concurrent.TimeUnit unit)
                         throws java.lang.InterruptedException
Throws:
java.lang.InterruptedException


Copyright © 2013. All Rights Reserved.