Search code examples
javamultithreadingthreadpoolexecutor

Execute a thread in a queue only if its field value doesn't match an already running thread


I have a large number of events coming in, and I must run the ones sequentially that have certain matching properties. Let's call this property "color". If I get 3 green events, and 2 yellow events, then I must run all the 3 green events in the order received, so they must be run one at a time. Same goes for the yellow events. If all 3 green events are processed before the first yellow event completes, that's fine, because across colors the order doesn't matter.

Now, I also have a limit to the total number of threads that can be run at once, and there's dozens of colors. So if I get 20 events of 20 different colors, I can't run them all at once. They must be under the thread pool limit. Ideally, the executor always be running the oldest event first, so long as there is no other event with the same color currently running.

So, what needs to happen is when it comes time for the ThreadPoolExecutor to dequeue a Runnable, it needs to check to see if there are any other Runnables of the same color. If so, look to the next Runnable in the Queue, until it finally finds one that doesn't have a matching color currently running. Then it will remove that from the Queue, without disturbing the order of any of the other elements in the Queue.

How would I go about implementing this way to dequeue threads in the ThreadPoolExecutor? Or is there another, better, way to do this?


Solution

  • Different colors can be processed concurrently, but same colors must be processed serially.

    So rather than trying to make all your threads communicate to one another what color they are doing let's just make a dedicated queue for each color, and one thread handling that queue.

    So one distributor thread reads the event queue and based on the color of the event posts it on the appropriate queue.

    Now you indicate that you can't have as many threads as there are colors, but colors can share a queue. That still guarantees the serial processing of each color.