[concurrency-interest] JDK 9 FutureTask's use of Thread.yield

Andrew Haley aph at redhat.com
Thu Feb 2 09:24:25 EST 2017


On 02/02/17 13:01, Dávid Karnok wrote:
> Maybe this was discussed before, but wouldn't it make more sense to have
> Thread.onSpinWait() at these locations (now that it is available) just by
> itself or as a code that leads to the current Thread.yield() approach?

They aren't really equivalent.  onSpinWait() should be used on x86
systems whenever there is a spin, but it doesn't actually yield.  A
common pattern would be something like

   while (state == INTERRUPTING && counter-- > 0) {
       Thread.onSpinWait();
   }
   while (state == INTERRUPTING) {
       Thread.yield();
   }

which does a hard spin for a while, then backs off to using yield().

BTW, I'm not sure that Thread.onSpinWait() has an implementation other
than x86, where it generates a PAUSE instruction.

Andrew.


More information about the Concurrency-interest mailing list