On Mar 10, 3:57 pm, "Steve W. Jackson" <stevewjack...@knology.net>
wrote:
> 1. There needs to be a delay before the reset happens. (AKA - The
> user has to have time to read the status message.)
On the first message in the status bar put the current time + 5 seconds (or
whatever delay you'd like) into a member variable of that inner class and
start the thread. When the thread is finished sleeping it checks whether
the current time is >= the end time you stored previously.
> 2. If I perform an undo and then immediately perform a redo, I don't
> want the first thread to continue its countdown. There should only
> ever be one active timer.
There will only ever be one thread. When a new message is shown in the
status bar, simple set the new end time to now() + your delay. When the
thread wakes up from the old sleep it checks for the current time, the end
time has not yet been reached, it goes back to sleep.
It's that simple. :)
David
Jason Cavett - 10 Mar 2008 22:15 GMT
On Mar 10, 4:48 pm, David 'Bombe' Roden <bo...@pterodactylus.net>
wrote:
> > 1. There needs to be a delay before the reset happens. (AKA - The
> > user has to have time to read the status message.)
[quoted text clipped - 16 lines]
>
> David
Haha...wow...yeah, that is simple. Thanks.
While I was working on this problem, I did run across the "Timer"
class. This seems to be a pretty nice way to solve this
issue...although, I guess I don't have the option of putting the
thread back to sleep, do I?
David Roden - 11 Mar 2008 12:46 GMT
> I guess I don't have the option of putting the thread back to sleep,
> do I?
When doing it manually you can simply call
Thread.currentThread().sleep(timeOfNextEvent - now);
In case of Timer (I assume java.util.Timer) you'd have to check in your
timer task that you still need to run it.
I'd recommend using wait() (and notify()) instead of sleep(), though. That
gives you the possibility to wake up the thread in case you e.g. want to
exit the thread and do not want it to wait for the next thirty minutes. :)
David
Gordon Beaton - 11 Mar 2008 12:51 GMT
> When doing it manually you can simply call
> Thread.currentThread().sleep(timeOfNextEvent - now);
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.
/gordon
--
David Roden - 11 Mar 2008 18:32 GMT
> 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 afterwards,
not deleting the currentThread() invocation. My bad. :)
David
Jason Cavett - 12 Mar 2008 15:44 GMT
> > Note that Thread.sleep() is a static method, so the use of
> > currentThread() in this context is redundant and misleading.
[quoted text clipped - 4 lines]
>
> 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);
}
}