Re: JavaMemoryModel: Problem with Thread.sleep

From: Bill Pugh (
Date: Sun Oct 24 1999 - 21:31:11 EDT

>The problem isn't with sleep. The problem is that the way load/read/use are
>defined (don't have the book at home, so I can't quote it) it is completely
>legal and even encouraged to write code that polls a variable modified by
>another thread without synchronization.

The existing JMM doesn't mention sleep, so it isn't completely clear.
A loop that simply busy waits to see a change from another thread,
with no synchronization inside the busy wait loop, is definitely
broken under the old memory model (and would be under my new model,
and I think any reasonable model).

The question is what does sleep do? Is it simple a most cost
effective way of waiting for a certain number of milliseconds than a
timing loop, or does it has synchronization semantics?
>Unsynchronized readers are required in Java because it provides no read locks,
>only write locks. This is probably due to its heritage as a language for
>embedded machines, which are mostly uniprocessors. On MPs, you need multiple
>reader/single writer concurrency control.

Unsynchronized readers are not guaranteed to work under the old memory model.
The fact that they often work is an accident.
>I won't be able to make OOPSLA and the JMM BOF, but I do have one suggestion:
>see if you can implement an efficient (concurrent reader) hash table on an MP
>in whatever models you propose.

Sounds reasonable. But whatever solution we come up with will require
that readers perform some kind of synchronization operation (either
locking or reading a volatile variable). Otherwise, it is impossible
to force the required communication on an SMP.

Another reason why Java should have a weak memory model, and not
allow unsynchronized reads or unsynchronized sleep loops: Any data
race detection tool is going to decide that an unsynchronized read,
or an unsynchronized sleep loop, is a data race. I don't see how a
tool could not flag them and still flag most errors. The only way
data race detection tools will be useful is if few, if any,
recommended idioms get flagged as data races.


JavaMemoryModel mailing list -

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