RE: JavaMemoryModel: Guaranteed semantics for Thread.yield()

From: Jerry Schwarz (jschwarz@us.oracle.com)
Date: Wed Jan 02 2002 - 13:12:48 EST


At 09:04 AM 1/2/2002, Doug Lea wrote:

>To restate what I think Bill is saying, in order to guarantee
>that
> volatile x = 0;
> while (x == 0) Thread.yield();
> ...
>does what any programmer would expect, we need to do one of the following:
> 1. Define memory semantics for yield()
> 2. Guarantee that an infinite number of yield()s causes at least one
> actual yield.

The definition of "actual yield" isn't clear. Could it include yielding to
a system thread (e.g. the garbage collector)? What about threads running at
lower priority than the given thread, must you yield to them?

>Can anyone think of any alternatives?

As an alternative to 2: There are no fairness guarantees.

This may not satisfy the naive expectations of "any programmer" because I
suspect "any programmer" believes Java guarantees "unblocked threads make
progress" even without yields. If so then this is just another case where
the naive programmers expectations about thready code are violated.

>I think that Bill is right that (2) is a better path.
>
>-Doug

-------------------------------
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