[concurrency-interest] Ordered puts and takes

Joe Bowbeer joe.bowbeer at gmail.com
Mon May 11 02:54:39 EDT 2009

Perhaps you meant:

Thread 1:

/* a */  queueA.put(obj1);
/* b */  queueB.put(obj2);

Thread 2:

/* c */  queueB.take();
/* d */  queueA.remove();

The BlockingQueue javadoc defines its memory consistency effects, making
this fair game for the c-i list:

"Actions in a thread prior to placing an object into a BlockingQueue
happen-before actions subsequent to the access or removal of that element
from the BlockingQueue in another thread."

Connecting the dots:

a happens before b (Thread 1 program order)
b happens before c (BlockingQueue spec)
c happens before d (Thread 2 program order)

Therefore a happens before d, and remove will not throw


On Sun, May 10, 2009 at 10:40 PM, Alex Miller wrote:

> This is maybe a better question for the jmm list, but I'll start here and
> you can tell me to stuff it if so.
> If you have 2 unbounded LBQs:
>  LinkedBlockingQueue queueA = new LinkedBlockingQueue();
>  LinkedBlockingQueue queueB = new LinkedBlockingQueue();
> and 1 producer thread puts objects on queue A then queue B (no other
> synchronization):
>  queueA.put(obj1);
>  queueB.put(obj2);
> and you have a consumer that reads those queues (no other synchronization):
>  Object seen1 = queueA.take();
>  Object seen2 = queueB.remove();
> Is
> it possible to see NoSuchElementException from the final remove()?  I
> think this boils down to a JMM question of whether the puts
> (which are changing the queues on separate internal locks but are
> sequentially ordered in the producer thread) *guarantee* an ordering in the
> consumer thread such that obj1 must show up on queueA before obj2 on
> queueB.
> My intuitive answer is yes but the actual answer might be that this is
> merely likely, not guaranteed.
> Thanks...
> Alex
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20090510/961ac74d/attachment.html>

More information about the Concurrency-interest mailing list