Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java have an indexable multi-queue thread pool?

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.

like image 651
00500005 Avatar asked Jul 31 '13 14:07

00500005


People also ask

How many thread pools are there in java?

We can create the following 5 types of thread pool executors with pre-built methods in java.

How does java support thread pool?

A thread pool reuses previously created threads to execute current tasks and offers a solution to the problem of thread cycle overhead and resource thrashing. Since the thread is already existing when the request arrives, the delay introduced by thread creation is eliminated, making the application more responsive.

What is Max thread pool size in java?

maxThreads is the largest the pool will be before the server starts queueing up requests. Tomcat defaults these to 25 and 200, respectively.

What is thread pool in java?

Java Thread pool represents a group of worker threads that are waiting for the job and reused many times. In the case of a thread pool, a group of fixed-size threads is created. A thread from the thread pool is pulled out and assigned a job by the service provider.


2 Answers

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.

like image 161
Alexei Kaigorodov Avatar answered Oct 12 '22 04:10

Alexei Kaigorodov


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

like image 20
yshavit Avatar answered Oct 12 '22 04:10

yshavit