File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes cancel tasks after a certain time 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 "cancel tasks after a certain time" Watch "cancel tasks after a certain time" New topic
Author

cancel tasks after a certain time

Doua Beri
Ranch Hand

Joined: Sep 24, 2009
Posts: 60
hi. I want to cancel running tasks from a ThreadPoolExecutor that have the execution time higher than x seconds? is there anything implemented to do this or I have to do this in a separate thread and count the time for every task.

Thank you
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

You will have to do this yourself, there isn't something already implemented. You might consider using a java.util.Timer with a new java.util.TimerTask to cancel a task when each task begins to execute. You could implement it in a subclass of java.util.concurrent.ThreadPoolExecutor where you override the beforeExecute() method to add a task to the timer to interrupt the running thread if the timeout occurs. You could then use the afterExecute() method to cancel the TimerTask if the timeout didn't occur or to log/report/notify that the timeout did occur.


Steve
Doua Beri
Ranch Hand

Joined: Sep 24, 2009
Posts: 60
Steve Luke wrote:You will have to do this yourself, there isn't something already implemented. You might consider using a java.util.Timer with a new java.util.TimerTask to cancel a task when each task begins to execute. You could implement it in a subclass of java.util.concurrent.ThreadPoolExecutor where you override the beforeExecute() method to add a task to the timer to interrupt the running thread if the timeout occurs. You could then use the afterExecute() method to cancel the TimerTask if the timeout didn't occur or to log/report/notify that the timeout did occur.



Thank you for your answer. I think this a very good and clean implementation. However I have another question: how do I cancel a running task(ThreadPoolExecutor task)? I'm not reffering about the timertask.

Should I use Thread.interrupt() or is another more clean solution.

Thanks

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

I typically use Thread.interrupt(), or some variation on it. You could map the Future the ThreadPoolExecutor generates and do Future.cancel(true), but that relies on Thread.interrupt() anyway - so I am not sure exactly what benefit it has. So you will need to check for Thread.interrupted() and stop processing when true.

Another option I have used in the past is to implement a stop() method inside my Runnable which sets a flag which the Runnable checks for. This is basically the same thing as Thread.interrupt() though:

Doua Beri
Ranch Hand

Joined: Sep 24, 2009
Posts: 60
thanks for your help Steve. I already implemented a solution with Thread.interrupt() but after I think I will make a few adjustaments because your example looks more cleaner.

Thanks again
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

The only issue here is IO. On some JVMs, sitting on an OS that supports interruptible IO -- IO operations will trigger an interrupted IO exception, so your task can then check to see if it should clean up and terminate.

On other OSes, interrupt() will not cause an interrupted IO exception, meaning your task will keep running until it gets data, which hopefully, your task can then later check, clean, and terminate (but this can happen much later). An alternative, is to send the interrupt() as before, but also close the file (or socket), etc. This will cause an interrupted IO exception with some JVMs, and other IO exceptions with other JVMs, but regardless, your task can immediately check, cleanup, and terminate.

Henry




Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Doua Beri
Ranch Hand

Joined: Sep 24, 2009
Posts: 60
Henry Wong wrote:The only issue here is IO. On some JVMs, sitting on an OS that supports interruptible IO -- IO operations will trigger an interrupted IO exception, so your task can then check to see if it should clean up and terminate.

On other OSes, interrupt() will not cause an interrupted IO exception, meaning your task will keep running until it gets data, which hopefully, your task can then later check, clean, and terminate (but this can happen much later). An alternative, is to send the interrupt() as before, but also close the file (or socket), etc. This will cause an interrupted IO exception with some JVMs, and other IO exceptions with other JVMs, but regardless, your task can immediately check, cleanup, and terminate.

Henry






hi Henry. It's a good note. The job that's running is reading from a socket. In windows vista the process is interrupted. I will need to check in linux and mac os to see what happens. Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cancel tasks after a certain time
 
Similar Threads
TimerTask.cancel() not killing tasks in the webcontainer
Exception in timer : IllegalStateException
Timers & TimerTasks
Client server
ExecutorService invokeAll - blocking?