[concurrency-interest] Serialized ThreadPoolExecutor

Norman Elton normelton at gmail.com
Mon Jul 20 11:58:06 EDT 2009


I've got a ThreadPoolExecutor that runs tasks. I would like certain
tasks that implement the LockingJob interface to have a "locking key":

public interface LockingJob {
   public Object getLockingKey();
}

In this instance, the queue would serialize any jobs sharing a locking
key. If two jobs come in with key "A", the first must finish executing
before the second begins. Meanwhile, tasks that do not share that key
continue to execute as expected.

I've done this before with a Map<Object,List<Runnable>> that maps a
locking key to a list of jobs queued up. When a job is added to the
queue, the executor first checks to see if a "holding queue" exists.
If so, the job is appended to the list. If not, it is executed
immediately. When a job is completed, another is popped off the
holding queue and executed. Problem is, I have to synchronize around
this holding queue, which seems to introduce a lot of overhead.

Is there a better way to accomplish what I'm after? Am I completely crazy?

Thanks,

Norman


More information about the Concurrency-interest mailing list