Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

Working with Threads

Thread view: 
Jason Cavett - 10 Mar 2008 19:46 GMT
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.
Steve W. Jackson - 10 Mar 2008 20:57 GMT
In article
<563a482e-918c-4d86-8c56-798d1711a948@n58g2000hsf.googlegroups.com>,

> 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
[quoted text clipped - 16 lines]
>
> 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.
Signature

Steve W. Jackson
Montgomery, Alabama

Jason Cavett - 10 Mar 2008 21:33 GMT
On Mar 10, 3:57 pm, "Steve W. Jackson" <stevewjack...@knology.net>
wrote:
> In article
> <563a482e-918c-4d86-8c56-798d1711a...@n58g2000hsf.googlegroups.com>,
[quoted text clipped - 29 lines]
>
> - 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.
David ‘Bombe’ Roden - 10 Mar 2008 21:48 GMT
> 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);
        }
    }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.