• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Timer use?

 
Author
Posts: 144
5
jQuery Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I may have to copyright "the thread you rode in on".
 
Tom Henricksen
Author
Posts: 144
5
jQuery Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Author
Posts: 144
5
jQuery Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic