Does Java have an indexable multi-queue thread poo

2019-06-17 05:52发布

Is there a Java class such that:

  1. Executable tasks can be added via an id, where all tasks with the same id are guaranteed to never run concurrently
  2. The number of threads can be limited to a fixed amount

A naive solution of a Map would easily solve (1), but it would be difficult to manage (2). Similarly, all thread pooling classes that I know of will pull from a single queue, meaning (1) is not guaranteed.

Solutions involving external libraries are welcome.

3条回答
\"骚年 ilove
2楼-- · 2019-06-17 06:14

If you don't find something that does this out of the box, it shouldn't be hard to roll your own. One thing you could do is to wrap each task in a simple class that reads on a queue unique per id, e.g.:

public static class SerialCaller<T> implements Callable<T> {
    private final BlockingQueue<Caller<T>> delegates;

    public SerialCaller(BLockingQueue<Caller<T>> delegates) {
        this.delegates = delegates;
    }

    public T call() throws Exception {
        return delegates.take().call();
    }
}

It should be easy to maintain a map of ids to queues for submitting tasks. That satisfies condition (1), and then you can look for simple solutions to condition (2), such as Executors. newFixedThreadPool

查看更多
劳资没心,怎么记你
3楼-- · 2019-06-17 06:22

I think that the simplest solution is to just have a separate queue for each index and a separate executor (with one thread) for each queue.

The only thing you could achieve with a more complex solution would be to use fewer threads, but if the number of indexes is small and bounded that's probably not worth the effort.

查看更多
Bombasti
4楼-- · 2019-06-17 06:23

For each id, you need a SerialExecutor, described in the documentation of java.util.concurrent.Executor. All serial executors delegate work to a ThreadPoolExecutor with given corePoolSize.

Opimized version of SerialExecutor can be found at my code samples.

查看更多
登录 后发表回答