Re: The code example in onSpinWait() Javadoc

Francesco Nigro via Concurrency-interest <[email protected]>
Newsgroups gmane.comp.java.jsr.166-concurrency
Message-ID <CAKxGtTXkFfck1K=o3-x2e4KNL0tG0-_RUrzVRnP1afc2z=OTcg@mail.gmail.com>
I would expect it on ConcurrentLinkedQueue::poll spin loop, but cannot say
if is "that better" :)
It doesn seem so weird to me, but  maybe:

given:

volatile E obj = null;
volatile boolean done = false;

publisher thread:

this.obj = //a valid non null value
this.done = true;

receiver thread:

E poll() {
    E o = this.obj;
    if (o == null && !done) {
        return null;
     }
     //o will be !null at some point
     do {
         if (o != null)
            return o;
         java.lang.Thread.onSpinWait();
     } while(true);
}

In case like this is more appropriate, maybe, but much less intuitive
probably.


Il giorno gio 14 nov 2019 alle ore 16:18 Nathan and Ila Reynolds via
Concurrency-interest <[email protected]> ha scritto:

> I suppose this begs the question for the community.  What would be the
> perfect example?
>
> On Thu, Nov 14, 2019 at 7:48 AM Roman Leventov via Concurrency-interest <
> [email protected]> wrote:
>
>> Thread.onSpinWait()'s Javadoc has the following code example:
>>
>>      class EventHandler {
>>          volatile boolean eventNotificationNotReceived;
>>          void waitForEventAndHandleIt() {
>>              while ( eventNotificationNotReceived ) {
>>                  java.lang.Thread.onSpinWait();
>>              }
>>              readAndProcessEvent();
>>          }
>>
>>          void readAndProcessEvent() {
>>              // Read event from some source and process it
>>               . . .
>>          }
>>      }
>>
>> Which doesn't seem to me as the best practice of using
>> Thread.onSpinWait().
>>
>> Thread.onSpinWait() is used in a very few classes in JDK itself, even
>> compared to other low-level mechanisms such as LockSupport.park/unpark.
>> It's hard to use it right. Cf. a recent thread "Low-latency pause in JDK"
>> in this mailing list.
>>
>> Maybe, in this case, it's better to not provide any usage example than to
>> provide such half-baked/"context and caveats" example, to prevent users
>> from copying this pattern from the highly visible Thread class's Javadocs?
>>
>> Expert users, the target audience for this method, unlikely benefit from
>> the present example either.
>> _______________________________________________
>> Concurrency-interest mailing list
>> [email protected]
>> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>>
>
>
> --
> -Nathan
> _______________________________________________
> Concurrency-interest mailing list
> [email protected]
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>

_______________________________________________
Concurrency-interest mailing list
[email protected]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.