Re #2: JavaMemoryModel: Problem with Thread.sleep

From: Joseph Bowbeer (jozart@csi.com)
Date: Sat Oct 23 1999 - 20:02:49 EDT


Comments, part 2.

>
> Thread 1: // command handling thread
>
> if (command.equals("quit") {
> terminateProgram = true;
> return;
> }
>
> Thread 2: // animation thread
>
> while (!terminateProgram) {
> try { Thread.sleep(100); }
> catch (Exception e) {}
> update state
> repaint();
> }
>

As written, I wouldn't be too incensed if the compiler or the JIT decided
that terminateProgram was constant inside thread2's loop. Also, I think
sleep(n) in these situations is bad style. wait(n) & notify should be used
instead, even though it's more complicated.

In any event, I agree with Doug: it's usually a bug (and always suspect) to
try to transmit a value between threads without synchronization.

Unfortunately, I estimate that 1% of applets that use threads are written
correctly. That includes Sun's code, the Java Developers Almanac, the new
JavaSpaces book, etc.

--
Joe Bowbeer

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



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