[concurrency-interest] Class striped/ordered Thread Pool

Aleksey Shipilev aleksey.shipilev at gmail.com
Fri May 11 01:59:19 EDT 2012


Glenn,

Ditto for David Holmes comment about the guarantees for execution
order with multiple threads in thread pool. You will pay for strict
ordering with proper load balancing though. I think you should be
looking at instantiating a couple of threadpools, and multiplex
incoming StripedRunners onto them. There's no need to have 1:1
threadpool:class relationship, you need only enforce that the same
class always end up in the same executor. Like this (in pseudocode):

public class StripedExecutor {

    private static final int COUNT = 16; // better be power of two

    private final Executor[] executors = ... // instantiate each
executor with single thread;

    public void submit(StripedRunner runner) {
         int index = shuffle(runner.getStripeClass().hashCode()) & (COUNT - 1);
         executors[index].submit(runner);
    }

    private int shuffle(int value) {
         return ...; // bit-shuffle value a bit, protecting from bad hashcodes
    }

}

I think it is a good idea to extend StripedRunner from Runnable
(should it be named StripedRunnable then?). Also, your stripe class
should have stable hashcode, or some other distinguishable numerical
characteristic.

-Aleksey.

On Fri, May 11, 2012 at 9:14 AM, Glenn McGregor <glenn at 8950aaa.com> wrote:
> On 5/10/2012 10:07 PM, David Holmes wrote:
>
> Not sure if this makes sense unless you use a SingleThreadExecutor per
> stripeClass. Otherwise you have races in the submission of StripedRunners of
> the same stripeClass, and even if you order the queue so that they start
> execution in the right order, you can't guarantee what order they will
> complete in. Unless it is all done by a single thread.


More information about the Concurrency-interest mailing list