JavaMemoryModel: Guaranteed semantics for Thread.yield()

From: Bill Pugh (pugh@cs.umd.edu)
Date: Mon Dec 31 2001 - 10:20:15 EST


In finishing up our writeup on fairness, I think we need to guarantee
some semantics for Thread.yield() and Thread.sleep().

Specifically, either operation should have the semantics that with
some finite probability, some other eligible thread of equal or
higher priority is allowed to execute at least one instruction.

On systems with full preemptive multithreading, this is a no-op,
since it is always the case that with some finite probability some
other eligible thread will be allowed to execute. However, on systems
that don't have preemptive multithreading, or that only allow context
switches at compiler defined points, this guarantee will have
semantics.

This guarantee will ensure that both of the following two examples
terminate, while without the guarantee they can both fail to
terminate.

Example 1:
Initially, volatile x = 0

Thread 1:
while (x == 0) {
        Thread.yield();
        }

Thread 2:
x = 1

Example 2:
Initially, x = 0 (x is non-volatile in this example).

Thread 1:
int r1;
do {
        Thread.yield();
        synchronized(this) {
          r1 = x;
           }
     } while (r1 == 0);

Thread 2:
synchronized(this) {
   x = 1
   }

-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:36 EDT