[concurrency-interest] Ordered puts and takes

Gregg Wonderly gregg at cytetech.com
Mon May 11 11:44:30 EDT 2009


and the easy fix for this is

qA.put();
qB.put();

... in the other thread ...

qB.take();
qA.take();

so that you always block, waiting for the last event that you need to have 
happen of multiple and then you get the implied ordering of operations needed.

The queue operations take care of visibility themselves.

Gregg Wonderly

David Holmes wrote:
> Hi Alex,
> 
> JMM visibility and ordering issues aside, you can easily get
> NoSuchElementException if the interleaving is as follows:
> 
>        Thread 1                 Thread 2
>    queueA.put(obj1);
>                       Object seen1 = queueA.take();
>                       Object seen2 = queueB.remove();
>    queueB.put(obj2);
> 
> 
> David Holmes
> 
>> -----Original Message-----
>> From: concurrency-interest-bounces at cs.oswego.edu
>> [mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of Alex
>> Miller
>> Sent: Monday, 11 May 2009 3:40 PM
>> To: concurrency-interest at cs.oswego.edu
>> Subject: [concurrency-interest] Ordered puts and takes
>>
>>
>>
>>
>> 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
>> _______________________________________________
>> Concurrency-interest mailing list
>> Concurrency-interest at cs.oswego.edu
>> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
> 
> 
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
> 
> 



More information about the Concurrency-interest mailing list