I got an assignment that I find it quite challenging and I have no idea how to start.
Here is the question:
I got a queue of random Products(consists of type A,B and C), says
P[A], P[A], P[B], P[C], P[C], P[A], P[B], P[B]….
I gonna send these product to ProcessProduct() concurrently but two same type of products can not be processed together.
Here is the scenario:
first P[A] get processed and the second P[A] will have to wait for the first one, and the third P[B] and the fourth P[C] will get processed right away, and the fifth P[C] will have to wait for the previous one….
Hope my description is not too confusing.
I will appreciate it if anyone could give me some suggestions.
Thank you.
Group your product list by type then process each group in parallel.
EDITED
This an example (if the product list is static):
This is another example for a dynamic queue of products to process (I didn’t run the code, read it as a kind of pseudo-code):
P.S. Instead of the lock you may use a ConcurrentDictionary.