[concurrency-interest] The code example in onSpinWait() Javadoc

David Lloyd david.lloyd at redhat.com
Thu Nov 14 10:19:35 EST 2019


A trivial spin lock?

On Thu, Nov 14, 2019 at 9:18 AM Nathan and Ila Reynolds via
Concurrency-interest <concurrency-interest at cs.oswego.edu> wrote:
>
> 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 <concurrency-interest at cs.oswego.edu> 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
>> Concurrency-interest at cs.oswego.edu
>> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
>
> --
> -Nathan
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest



-- 
- DML



More information about the Concurrency-interest mailing list