At 10:32 AM 6/22/2001, Doug Fields wrote:
>C'mon guys...
>
>Write a singular animation thread. When it's stopped, have it "wait" in a 
>synchronized block, and have the starting call "notify" it. Why use a busy 
>loop when Java provides all these perfect concurrency facilities?

Argh, actually, there's a problem:

running could be out of date as it's not synchronized when checked.

You can handle this by doing something like:

done = false;
while (!done) {
         ...
         synchronized (this) {
                 done = !running;
         }
}

Or take the lazy way out and make it volatile. :)

Cheers,

Doug



>Something like: (pseudo code)
>
>run() {
>         try {
>                 while (true) {
>                         synchronized (this) {
>                                 while (!running) wait();
>                         }
>                         while (running) {
>                                 // Sometimes you miss interruptions...
>                                 if (Thread.interrupted())
>                                         throw new InterruptedException();
>                                 showNextFrame();
>                                 Thread.sleep(ANIMATION_DELAY);
>                         }
>                 }
>         } catch (InterruptedException ie) {
>                 // Done
>         }
>}


_______________________________________________
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing

Reply via email to