for my current development, I have many threads ( Producers) that create Tasksand many threads that consume these Tasks( consumers)
Each is Producersidentified by a unique name; A Tasksconsists of:
My question is about the data structure used by ( Producers) and ( consumers).
Parallel queue?
We could naively imagine that it Producersfills a parallel queue with Tasksand ( consumers), reads / consumes Tasks, stored in a parallel queue.
I think this solution scales well enough, but one case is problematic: if it Producerscreates very quickly two Taskswith the same name but not with the same data (both tasks T1 and T2 have the same name but T1 has data D1 and T2 has data D2 ), it is theoretically possible that they are consumed in the order of T2, then T1!
Task Map + Queue?
Now I imagine creating my own data structure (say MyQueue) based on Map + Queue. For example, in a queue, it will have a method pop()and push().
- The method
pop()will be quite simple. - Method
push():- Make sure the existing
Taskone is not yet inserted in MyQueue(running find()on the Map)- : ,
Task to-be-insert, , Task - :
Task ,
, ... , , ; , .
, ?
, , , .