I've been successfully using ExecutorService implementations to manage thread pools.
Now I've found a situation where dozens of concurrent threads are used to insert large data objects into a data warehouse. Data may come from different sources. When two or more threads are inserting data from the same source, deadlocks happen at a certain rate. These deadlocks are well understood and can only be avoided by not having two threads with data from the same source at the same time.
I have read about the internals of the ExecutorService interface and found the abstract class AbstractExecutorService can be a good starting point. I also imagine that overriding the newTaskFor method could be enough for the objective.
I am a bit lost about how to label the submitted threads with the name of the source, to later choose a good one in newTaskFor based on that label. The main idea is to run a submitted thread only if its label is unique among the running threads. The number of running threads will be of course limited by the miminum of (maximum pool size, current label diversity) at any time.
Any help on this? Thanks on advance.