another question: I have an object that uses a PriorityBlockingQueue. This object is shared among several threads that invoke add( object ) and pool() on this shared object. here is the code:
Will I have problems if one thread calls MyClass.pool() at the same time another thread calls MyClass.add() or the structure of PriorityBlockingQueue takes care of it?
another question: I have an object that uses a PriorityBlockingQueue. This object is shared among several threads that invoke add( object ) and pool() on this shared object. here is the code:
Will I have problems if one thread calls MyClass.pool() at the same time another thread calls MyClass.add() or the structure of PriorityBlockingQueue takes care of it?
I was a mistyped, I mean poll() method "Retrieves and removes the head of this queue, or returns null if this queue is empty."
Regards,
Then, no, you will have no problems. The PriorityBlockingQueue will have a lock when it is necessary to protect itself in concurrent situations. As Ireneusz said, the PriorityBlockingQueue implements BlockingQueue, which states:
BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control. However, the bulk Collection operations addAll, containsAll, retainAll and removeAll are not necessarily performed atomically...
<edit>
The above is about the Queue itself - it will be protected and safe to perform modifying actions on in concurrent threads. But the Object that you put into the Queue may not be safe. For example, you could run in to problems if both the add()ing thread and the poll()ing thread modify the Message, or if one thread modifies the Message while the other reads its. As always you will have to make your own code thread safe yourself.
This message was edited 1 time. Last update was at by Steve Luke
shivendra tripathi
Ranch Hand
Joined: Aug 26, 2008
Posts: 263
posted
0
Will I have problems if one thread calls MyClass.pool() at the same time another thread calls MyClass.add()
It can never happen because both the method pool and add is synchronized. To answer this I don't need to know how PriprityBlockingQueue behave.