Reputation: 23
I want to implement the queue, which waits for shared resource. When this resource is free, queue take the first added element and change the info in this resource. But when no, just wait. Is there any queues in concurrency library that can do this? Or i need to write my own queue? Or is there are a better way to do this? Thanks for help)
Upvotes: 1
Views: 549
Reputation: 7232
It sounds like you want two things.
A BlockingQueue
instance (such as ArrayBlockingQueue
) for your queue of tasks to perform on the resource.
A lock for the resource for your threads to wait on until the resource is available.
Your threads can then either take an item off the queue and wait on the resource lock, or wait on the resource lock and then try to take an item off the queue while holding that lock.
Waiting and taking from the queue first is the easier design, but your wording in the question makes it seem like you wish to do the opposite and wait on the resource lock first and then try to take from the queue. That workflow is a little bit more complicated in that you should poll()
from the queue (instead of take()
'ing) and release your resource lock if you do not want to hold it if the queue is empty.
E.g.
poll
from queueUpvotes: 1