Wednesday, 11 September 2013

Unable to maintain order of producer tasks in java multithreading

Unable to maintain order of producer tasks in java multithreading

I am writing a multithreaded application where there a n producers who
tries to add an element to the shared resource. I want to maintain the
order in which producer produces the element in the shared resource.
For example my shared resource is a SynchronizedQueue and P1, P2, P3, P4
are going to produce a new elements in the order p1, p2, p3, p4 and during
that time P5 producer is adding its element to the queue, so P1, P2, P3,
P4 will be waiting for the lock. Once P5 releases the lock any one of the
P1-4 will acquire the lock, so we loose the order of elements.
Is there a way to maintain the order of elements who wait for a lock ?
From my understanding it is not possible but I would like to check whether
this can be achieved programmatically.

No comments:

Post a Comment