Working with Threads

J

Jason Cavett

I'm trying to implement a status bar whose status is updated depending
on what the user is doing in the system. That is working well at this
point. However, I am now trying to implement a way that the status
bar resets itself after a certain amount of time (back to the "Ready"
status). So, it'll work something like this...

1. User Does Action
2. Status Bar Changes
3. Timer Thread Starts
4. When time has passed, status bar is reset.
5. If the user has done another task before the time has passed, the
time is reset back to its starting countdown value and the count down
starts again.

I am having a heck of time with part 5. I have created an inner class
(extends Thread) and I can't seem to find a way to check to see if the
thread is still sleeping and then reset that time back to the original
sleep time if it is.

Any help would be appreciated. Thanks.
 
S

Steve W. Jackson

Jason Cavett said:
I'm trying to implement a status bar whose status is updated depending
on what the user is doing in the system. That is working well at this
point. However, I am now trying to implement a way that the status
bar resets itself after a certain amount of time (back to the "Ready"
status). So, it'll work something like this...

1. User Does Action
2. Status Bar Changes
3. Timer Thread Starts
4. When time has passed, status bar is reset.
5. If the user has done another task before the time has passed, the
time is reset back to its starting countdown value and the count down
starts again.

I am having a heck of time with part 5. I have created an inner class
(extends Thread) and I can't seem to find a way to check to see if the
thread is still sleeping and then reset that time back to the original
sleep time if it is.

Any help would be appreciated. Thanks.

Look into the wait/notify mechanism. The idea would be for some
external thing (that action you mention) to notify the sleeping thread
that something important is happening. The response of your thread
would be to wake up and reset itself.
 
J

Jason Cavett

Look into the wait/notify mechanism.  The idea would be for some
external thing (that action you mention) to notify the sleeping thread
that something important is happening.  The response of your thread
would be to wake up and reset itself.
--
Steve W. Jackson
Montgomery, Alabama- Hide quoted text -

- Show quoted text -

That would work, except I need a couple things to happen:

1. There needs to be a delay before the reset happens. (AKA - The
user has to have time to read the status message.)

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.
 
D

David ‘Bombe’ Roden

Jason said:
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
 
J

Jason Cavett

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.


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

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?
 
D

David Roden

Jason said:
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
 
G

Gordon Beaton

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

--
 
D

David Roden

Gordon said:
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
 
J

Jason Cavett

Oops, yes. I wrote wait() before that and corrected (sort of) it afterwards,
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);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top