permaculture playing cards*
The moose likes Threads and Synchronization and the fly likes Question involving InterruptedException and sleeping in a thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Question involving InterruptedException and sleeping in a thread" Watch "Question involving InterruptedException and sleeping in a thread" New topic
Author

Question involving InterruptedException and sleeping in a thread

Steve Stevenson
Greenhorn

Joined: Apr 12, 2012
Posts: 21
I have a thread that basically loads an image from a disc, waits for a period of time to expire, and then loads another image. The duration between images is user-defined and can be adjusted within the program. I'm trying to figure out the proper way to do this.


Currently I have what I consider a bit of a kludge. Say the duration is 10 seconds, what I do is sleep for 1 second intervals in a loop and constantly check on each loop to see if I've exceeded the user's delay preference. This works, but is this inefficient?


What I think is the proper way is to use the interrupt() command and handle its InterruptedException exception. That way I would initially put that thread to sleep for the full 10 second period and then if the user changes the duration I would interrupt the thread, calculate the amount of time necessary to sleep for the user's new preference (assuming the duration was extended), and then sleep for that amount.


The only problem with going in this direction is that I don't know why InterruptedException occurred. Aside from the user changing the duration, any other time I get this InterruptedException I want to handle this by ending the thread. I know I can use some custom flags to kludge my way around this, but removing kludges is the goal here.


Any recommendations?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3577
    
  14

Submitting Callables to an ExecutorService seems perfect for this task.

Create a Callable class that returns an image from its call() method. You can then cancel and timeout the task through the Future class.
Here you can change the timeout between loading images in another thread (make timeout and timeunit volatile) and they will affect the next image being loaded. You should allow the user to cancel a task explicitly by providing a cancel button that calls currentTask.cancel().
Steve Stevenson
Greenhorn

Joined: Apr 12, 2012
Posts: 21
Stephan van Hulst wrote:Submitting Callables to an ExecutorService seems perfect for this task.

Create a Callable class that returns an image from its call() method. You can then cancel and timeout the task through the Future class.
Here you can change the timeout between loading images in another thread (make timeout and timeunit volatile) and they will affect the next image being loaded. You should allow the user to cancel a task explicitly by providing a cancel button that calls currentTask.cancel().


Wow, I have never seen that class before so I will have to take a look at what that is. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question involving InterruptedException and sleeping in a thread
 
Similar Threads
interrupt() without sleep()
plz explain me interrupt(), isInterrupted(), interrupted() method
one more question from Dan's questions
Q on thread at Jxam
K&B Ch. 9: two Questions