• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PriorityBlockingQueue and concurrent access

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Nitesh!
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic