Does Java have an indexable multi-queue thread pool?

一曲冷凌霜 提交于 2019-12-01 15:54:20

问题


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.


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/17972884/does-java-have-an-indexable-multi-queue-thread-pool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!