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?
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
}
}
Then, all you have to do to exit your program is:
animThread.interrupt();
(Or create the thread as a daemon)
And to stop your thread:
synchronized stop() {
running = false;
}
And to re-start it (after you've new Thread(this).run() of course)
synchronized start() {
running = true;
notifyAll(); // In case you have more than one animation thread,
or notify() if just one
}
Voila. Simple.
(cf Doug Lea's book for more info, Java Concurrency, or Holub's or any of
several others)
Cheers,
Doug
>A third suggestion is to have a thread that loops, and in the loop have an
>if statement that tests a boolean variable. If it is false, don't do
>anything. This flag should be volatile, and your start/stop buttons need
>only change its value, for you to achieve the desired effect. You may need
>to sleep for a bit if the animation is stopped (to prevent CPU overload) -
>but, suck and see.
>
>BTW, this is NOT a Swing-related question. I guess I should give up now, WRT
>ppl politely observing the wishes of listmembers, though.
>
>-----Original Message-----
>From: Singh,Gary - PLANO <[EMAIL PROTECTED]>
>To: 'D. Michael Nelson ' <[EMAIL PROTECTED]>
>Cc: '[EMAIL PROTECTED]' <[EMAIL PROTECTED]>
>Date: Thursday, June 21, 2001 07:01
>Subject: RE: Animation Thread?
>
>
> >Two ways I can think off. In your run method you will have to set up a flag
> >which is checked
> >
> >say you can put the entire thing in say
> >
> >while( flag )
> >{....
> >}
> >
> >So your run methods exits when the flag is put to false in the
> >actionPerformed() method of the mouseListener on your buttons.
> >
> >OR : have the mouseListener call
> >
> >your own start and stop methods which causes this thread to stop and start
> >by calling Thread.wait() and notifyAll() ( mind you these have to a lock of
> >some object or else the complier will complain...). Slight tricky here so
>be
> >slightly careful in implementation
> >
> >Thanks and Regards,
> >Gary Grewal
> >
> >
> >
> >-----Original Message-----
> >From: D. Michael Nelson
> >To: [EMAIL PROTECTED]
> >Sent: 6/20/01 12:19 PM
> >Subject: Animation Thread?
> >
> >Hello,
> >
> >I have created a thread that runs an animation. I want to add two
> >JButtons
> >to the application that will start and pause the animation thread. I am
> >uncertain how to pause the animation thread or resume it? Any ideas?
> >
> >Thanks In Advance,
> >D. Michael Nelson
> >
> >
> >_______________________________________________
> >Swing mailing list
> >[EMAIL PROTECTED]
> >http://eos.dk/mailman/listinfo/swing
> >_______________________________________________
> >Swing mailing list
> >[EMAIL PROTECTED]
> >http://eos.dk/mailman/listinfo/swing
>
>_______________________________________________
>Swing mailing list
>[EMAIL PROTECTED]
>http://eos.dk/mailman/listinfo/swing
_______________________________________________
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing