wood burning stoves 2.0*
The moose likes Java in General and the fly likes Timer use? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Timer use?" Watch "Timer use?" New topic
Author

Timer use?

Tom Henricksen
Ranch Hand

Joined: Mar 23, 2004
Posts: 136

I need to call a method that may take some time. If it takes more than 3 minutes I need to return a boolean false.



Would this be a good place for a java.util.Timer?
Thanks,
Tom
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think you'll need two threads here, one to do the work and one to keep track of time. Timer runs on another thread alright but I'm not sure what we'd want to do when it expires. How about putting the work onto the other thread and having the thread we rode in on check the time. I've done this before:

If you're in JDK 5 look into Future, too. It has a method to get the results from a Runnable with a timeout.

BTW: I might move throwing the exception into the runnable ... then it would more like Future ...

Does that ring your chimes at all?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I may have to copyright "the thread you rode in on".
Tom Henricksen
Ranch Hand

Joined: Mar 23, 2004
Posts: 136

I think that will work for me Stan. Or ring my Chimes!

I will play around with that idea and get it working.

Thanks,
Tom
Tom Henricksen
Ranch Hand

Joined: Mar 23, 2004
Posts: 136

Sorry for the delay. This appears to work for our application. How would you go about testing this(the join)? Shouldn't I be able to make the calling method take too long and then this method should return false?



Thanks,
Tom
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, welcome back. We can test the timeout with a test double, kind of like a stunt double in the movies. Since your variable is a Runnable type, we can give it any Runnable.

Now it looks like we should pass the Runnable into this method instead of creating it in place. Then we can test the method with any Runnable. Making classes testable leads to some neat designs. Show us what you make next.
 
wood burning stoves
 
subject: Timer use?