Re: Working with Threads
On Mar 11, 1:32 pm, David Roden <d...@ina-germany.de> wrote:
Gordon Beaton wrote:
Note that Thread.sleep() is a static method, so the use of
currentThread() in this context is redundant and misleading.
Thread.sleep() always acts on the calling thread.
Oops, yes. I wrote wait() before that and corrected (sort of) it afterward=
s,
not deleting the currentThread() invocation. My bad. :)
David
In case anybody's curious, here is the code I used to get this to
work:
statusCounter is an AtomicInteger.
Basically, if the id matches the counter, the message will be reset.
If the message has been changed before the TimerTask is fired off,
though, the message will not be cleared leaving it to a later message.
I use an Observer pattern (hence the notification) to update any
status bars/tooltips/etc (hence,
this.notify(MainModel.UPDATE_STATUS) ).
Works pretty well. Thanks again for your help.
public void setStatusMessage(String statusMessage, long time) {
this.statusMessage = statusMessage;
this.notify(MainModel.UPDATE_STATUS);
final int id = statusCounter.incrementAndGet();
if (time != 0) {
TimerTask task = new TimerTask() {
/**
* @see java.util.TimerTask#run()
*/
@Override
public void run() {
if (statusCounter.compareAndSet(id, 0)) {
clearStatusMessage();
}
}
};
timer.schedule(task, time);
}
}