Re: thread wait
korcs wrote:
I have a problem with suspending a thread.
It's really difficult to see what is happening with an incomplete
example. As Patricia mentioned, try to come up with a small but complete
program to demonstrate the problem.
Anyway, my thoughts on what you have selected:
As suspend() has deprecated I am trying to use the wait() - notify()
combination, but after the thread pauses it won't resume again.
Yes, look at the explanation. suspend can pause a thread whilst it is
holding a lock that you may not even be aware (perhaps in some library,
perhaps loading classes).
class startListener implements ActionListener {
Class names should have initial caps.
Tabs work badly. Particularly in Usenet. My reader is displaying you
post indented inconsistently. Quoting doesn't help either.
if(!clock.isAlive()) {
clock.start();
So clock is a thread. You can only start threads once. isAlive isn't a
good check. There is also a potential race from between isAlive, start
and the thread actually starting.
threadsSuspended = true;
Technically this should be in a synchronised block matching reading of
the variable (you can use volatile, but that is very difficult to get
right).
public void run()
{
I'd suggest using an inner class, even if it just calls a private method
on the outer class.
Note that 'this' within an inner class refers to inner instance, not the
outer. Even Brian Goetz managed to publish a book with this mistake.
this.putText(new Integer(counter).toString());
I don't know the implementation of putText, but note that Swing
components should always be accessed on the AWT Event Dispatch Thread (EDT).
Integer.toString(counter) would be a better way of expressing that (or
String.valueOf(counter) if you must).
Thread.sleep(clock_period);
synchronized(this) {
while (threadsSuspended)
wait();
}
Are you sleeping or waiting? (Unless it's a bug demonstration, I always
prefer timed wait to sleep as you can quite a wait without having to use
interrupt (which has some problems associated with it)).
} // while
} catch (InterruptedException e) {
}
Yay! Thread interrupts that actually interrupts rather than being
dropped and carrying on as normal.
return;
Unnecessary.
Tom Hawtin