What is a common approach to in-order multi-threaded message processing?
Consider the following example: I have a publisher that sends numbers into a queue: 1, 2, 3, 4, 5, 6, 7
My goal is to process odds and evens sequentially.
One possible solutions I know is to have a separate queue per thread and split original based on n % m
criteria.
What I'm worried about is the fact that numbers may be distributed unevenly and I will end up with some threads to have less work to do.
I have been thinking of implementing custom queue that will check if queue element with same criteria is being processed by some other thread and if it does, try to find another one. That might work, I have tried to implement something, but it gets complicated and is harder to test. That is why I first try to find existing solutions to the problem.
Not an answer but too long for a comment.
My goal is to process odds and evens sequentially.
In that case, you can not have more than one thread for odds and one for evens. Any reasons why you need a sequential run? Do you use the result of process(2)
to run process(4)
?
What I'm worried about is the fact that numbers may be distributed unevenly and I will end up with some threads to have less work to do.
Possibly, but how could you distribute more work to the idle thread without breaking the sequential constraint?
If you have 2 types and each has to be processed per-type-squentially you can have only 2 threads. If there is no message of the other type to process only 1 thread can work.
In that case use 2 queues and put messages based on type in there & let each thread consume one queue. You could use a third thread to distribute the messages maybe but in case one thread has a full queue you have to wait until you can consume messages from the original producer unless you have a way to request each type individually or you can throw away messages. You are limited by your own constraints here.
Beyond the theoretical part you may want to look at BlockingQueues & ExecuterServices like in that answer: Producer/Consumer threads using a Queue
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.