[concurrency-interest] Ordered puts and takes

Alex Miller alexdmiller at yahoo.com
Mon May 11 01:40:22 EDT 2009


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


More information about the Concurrency-interest mailing list