[concurrency-interest] Class striped/ordered Thread Pool

√iktor Ҡlang viktor.klang at gmail.com
Wed May 16 04:43:30 EDT 2012

On Wed, May 16, 2012 at 10:16 AM, Dr Heinz M. Kabutz <
heinz at javaspecialists.eu> wrote:

> **
> I've spent yesterday and part of today looking at various approaches to
> solve this problem.
> 1. My idea of a striped queue would not work, because tasks are not always
> enqueued before they are handed off to workers.

Ah, the Curse of Order!

> 2. I've got a nice solution utilizing the SerialExecutor approach,
> however, as Joe mentioned, shutdown is particularly challenging.  I'm still
> trying to solve that, but it is not easy.

Executor has no shutdown ;-)

> 3. Another challenge, also mentioned already in this thread, is that we
> need to carefully manage the map so that we do not get a memory leak.  The
> solution I'm trying now is to delete the SerialExecutor from the map
> whenever it is empty.  This might create a lot of objects, but at least we
> don't have to worry about a memory leak.

If you have the SerialExecutor extend CLQ and use a @volatile int/boolean
to keep track of "is scheduled" or not then it's essentially only 2
allocations to create the SerialExecutor:

1 for the head-node in the CLQ and one for the SerialExecutor itself, so it
shouldn't be that bad.

However, the performance will of course vary depending on how it's used.


> Regards
> Heinz
> --
> Dr Heinz M. Kabutz (PhD CompSci)
> Author of "The Java(tm) Specialists' Newsletter"
> Sun Java Champion
> IEEE Certified Software Development Professionalhttp://www.javaspecialists.eu
> Tel: +30 69 75 595 262
> Skype: kabutz
> On 5/13/12 4:54 AM, Joe Bowbeer wrote:
> On Fri, May 11, 2012 at 12:22 PM, Andrew Trumper wrote:
>> BoundedExecutor 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 hangin' 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.)
>  The SerialExecutor that I referred to is in the Executor javadoc.
> http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executor.html
>  Here is a link to one of the times that a similar problem was discussed
> previously on c-i:
>  http://cs.oswego.edu/pipermail/concurrency-interest/2010-June/007208.html
>  This post refers to both SerialExecutor and a (different)
> BoundedExecutor.
>  There are several different SerialExecutor and BoundedExecutor
> implementations floating around.  In one way, this validates the usefulness
> of the simple Executor interface!  In real world applications, I wonder if
> a single BoundedExecutor implementation would be useful.  Shutdown is one
> of the missing pieces in these sample implementations -- and this is
> especially difficult to manage when there are multiple, delegating
> executors.  I expect that different trade-offs would be chosen in different
> situations, in which case it may be hard to provide a general solution.
>  --Joe
> ------------------------------
> _______________________________________________
> Concurrency-interest mailing listConcurrency-interest at cs.oswego.eduhttp://cs.oswego.edu/mailman/listinfo/concurrency-interest
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> 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/20120516/3e871796/attachment-0001.html>

More information about the Concurrency-interest mailing list