File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Timer use?

 
Tom Henricksen
Author
Ranch Hand
Posts: 136
Eclipse IDE jQuery Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • 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)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I may have to copyright "the thread you rode in on".
 
Tom Henricksen
Author
Ranch Hand
Posts: 136
Eclipse IDE jQuery Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 136
Eclipse IDE jQuery Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • 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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic