I have been reading about the thread-pool pattern and I can\'t seem to find the usual solution for the following problem.
I sometimes want tasks to be executed serial
If I understand the problem correctly, the jdk executors don't have this capability but it's easy to roll your own. You basically need
ExecutorService
)The difference to the jdk executors is that they have 1 queue with n threads but you want n queues and m threads (where n may or may not equal m)
* edit after reading that each task has a key *
In a bit more detail
key.hashCode() % n
or it could be some static mapping of known key values to threads or whatever you wantit's easier enough to add auto restarting worker threads to this scheme, you just then need the worker thread to register with some manager to state "I own this queue" and then some housekeeping around that + detection of errors in the thread (which means it unregisters the ownership of that queue returning the queue to a free pool of queues which is a trigger to start a new thread up)