[concurrency-interest] Class striped/ordered Thread Pool

√iktor Ҡlang viktor.klang at gmail.com
Sat May 12 06:09:48 EDT 2012


Here's an implementation I threw together (haven't been tested yet so YMMV)
that should make any Executor into a striped one, for every runnable that
is put in there which is Striped.

https://gist.github.com/2665603

Only locks that are involved should be the ones in ConcurrentHashMap,I
could opt for Scala's concurrent.TrieMap but wanted to stay 100% JDK for
this exercise.

Cheers,
√

On Fri, May 11, 2012 at 9:22 PM, Andrew Trumper <andrew at intelerad.com>wrote:

> 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<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.
>
>
> ______________________________**_________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.**oswego.edu <Concurrency-interest at cs.oswego.edu>
> http://cs.oswego.edu/mailman/**listinfo/concurrency-interest<http://cs.oswego.edu/mailman/listinfo/concurrency-interest>
>



-- 
Viktor Klang

Akka Tech Lead
Typesafe <http://www.typesafe.com/> - The software stack for applications
that scale

Twitter: @viktorklang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20120512/318ebe09/attachment-0001.html>


More information about the Concurrency-interest mailing list