• Post Reply Bookmark Topic Watch Topic
  • New Topic

PriorityBlockingQueue and concurrent access  RSS feed

 
Rafael Z. Frantz
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi folks,

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:


The PriorityBlockingQueue is used like this:



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?

Thanks a lot!

 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not related to Distributed Java. Please CarefullyChooseOneForum while posting.
Moving to thread and synchronization.
 
Rafael Z. Frantz
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nitesh!
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rafael Z. Frantz wrote:
Hi folks,

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:


The PriorityBlockingQueue is used like this:



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?

Thanks a lot!



What does queue.pool() do? I don't see it in the PriorityBlockingQueue's API.
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:
What does queue.pool() do? I don't see it in the PriorityBlockingQueue's API.

Look in the BlockingQueue API, PriorytyBlockingQueue implements this interface.
 
Rafael Z. Frantz
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve,

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,
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rafael Z. Frantz wrote:Hi Steve,

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.
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!