Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

cancel tasks after a certain time

 
Doua Beri
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Doua Beri
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23855
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Doua Beri
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!