aspose file tools
The moose likes Threads and Synchronization and the fly likes  PriorityBlockingQueue and concurrent access Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark " PriorityBlockingQueue and concurrent access" Watch " PriorityBlockingQueue and concurrent access" New topic
Author

PriorityBlockingQueue and concurrent access

Rafael Z. Frantz
Greenhorn

Joined: Mar 09, 2010
Posts: 20

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

Joined: Feb 25, 2007
Posts: 1624

This is not related to Distributed Java. Please CarefullyChooseOneForum while posting.
Moving to thread and synchronization.


apigee, a better way to API!
Rafael Z. Frantz
Greenhorn

Joined: Mar 09, 2010
Posts: 20
Thanks Nitesh!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 1811

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.


Steve
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 412
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

Joined: Mar 09, 2010
Posts: 20
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

Joined: Jan 28, 2003
Posts: 1811

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.

This message was edited 1 time. Last update was at by Steve Luke

shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
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.


SCJP 1.5(97%) My Blog
 
 
subject: PriorityBlockingQueue and concurrent access
 
MyEclipse, The Clear Choice