[concurrency-interest] Class striped/ordered Thread Pool

Andrew Trumper andrew at intelerad.com
Fri May 11 15:22:27 EDT 2012


Hi Glenn,

If I understand what you're asking for, we do that sort of thing fairly 
frequently at Intelerad, In our case it's more like

  interface StripedRunner {
       Object getKey(); // anything that can be used as a key in a map
       Runnable getRunnable();
  }

You'd need something like:

public class PerKeyExecutor {
   private final mMap<Object, Executor> mExecutors = new HashMap<>();
   private static final Executor mThreadCache =
       new ThreadPoolExecutor( 5, Integer.MAX_VALUE, 60L,
                               TimeUnit.SECONDS,
                             new SynchronousQueue<Runnable>() )

   public synchronized void execute( StripedRunner runnable ) {
     Executor executor = mExecutors.get( runnable.getKey() );
     if ( executor == null ) {
       mExecutors.put( runnble.getKey(), createExecutor() );
       executor = mExecutors.get( runnable.getKey() );
     }
     executor.execute( runnable.getRunnable() );
   }

   private Executor createExecutor() {
      /*
       * create a wrapper that bounds the number of
       * runnables executing at once to 1
       */
     return new BoundedExecutor( 1, mThreadCache );
   }

   // clean-up methods omitted
}

BoundedExcutor is a simple wrapper that limits the number of 
concurrently running tasks to some number. It doesn't allocate any 
threads itself (unless you want it to) but uses the "mThreadCache". It's 
great because it means your whole application can use one thread cache 
but you can create little thread pools all over the place and not worry 
about having a bunch of idle threads hanggin' around. We use this all 
the time in our product.

(Note, that Joe Bowbeer has mentioned a class called SerialExecutor that 
seems to do the same thing. ?)

BoundedExecutor is part of the nuggu library, BSD license:

<http://nuggu.svn.sourceforge.net/viewvc/nuggu/HEAD/src/com/intelerad/tools/lib/concurrent/BoundedExecutor.java?revision=10&view=markup>

It would be really great to improve this class and put it in a library 
someone's actually heard of :-P.

(In our product we sometimes use a second BoundedExecutor around the 
thread cache to limit the concurrency to the number of cores/processors.)

- Andrew

On 05/11/2012 12:53 AM, Glenn McGregor wrote:
> Hello all,
>
> I'm looking for a thread pool with the following characteristics... (in
> pseudo-code)
>
> interface StripedRunner {
>      Object getStripeClass();
>      void run();
> }
>
> magicPool.execute( stripedRunner );
>
> The idea being that any StripedRunners with the same stripeClass would
> get executed in the order they were queued,
> but StripedRunners with different stripedClasses could execute
> independently.
>
> Is there a way to configure the standard concurrent pools to work this
> way (doesn't seem to be), or a way to extends
> the current code?
>
> Or perhaps some third party library implementation?

-- 

This email or any attachments may contain confidential or legally 
privileged information intended for the sole use of the addressees. Any 
use, redistribution, disclosure, or reproduction of this information, 
except as intended, is prohibited. If you received this email in error, 
please notify the sender and remove all copies of the message, including 
any attachments.



More information about the Concurrency-interest mailing list