ExecutorService that interrupts tasks after a timeout?

You can use a ScheduledExecutorService for this. First you would submit it only once to begin immidiatly and retain the future that is created. After that you can submit a new task that would cancel the retained future after some period of time.

You can use a ScheduledExecutorService for this. First you would submit it only once to begin immidiatly and retain the future that is created. After that you can submit a new task that would cancel the retained future after some period of time.

ScheduledExecutorService exectutor = Executors. NewScheduledThreadPool(2); final Future handler = exectutor. Submit(new Callable(){... }); exectutor.

Schedule(new Runnable(){ public void run(){ handler.cancel(); } }, 10000, TimeUnit. MILLISECONDS); This will execute your handler (main functionality to be interrupted) for 10 seconds, then will cancel (ie interrupt) that specific task.

I'd rather not have tons of cleanup tasks waiting to run only to find out their assigned task has already completed. There'd need to be another thread monitoring the Futures as they finish to remove their cleanup tasks. – scompt.

Com May 3 '10 at 15:18 The executor will only schedule this cancel once. If the task is completed then the cancel is a no op and work continues unchanged. There only needs to be one extra thread scheudling to cancel the tasks and one thread to run them.

You could have two executors, one to submit your main tasks and one to cancel them. – John Vint May 3 '10 at 15:23 You could hook on InterruptedException. Also see this answer.

– BalusC May 3 '10 at 15:24 That's true, but what if the timeout is 5 hours and in that time 10k tasks are executed. I'd like to avoid having all those no-ops lying around taking up memory and causing context switches. – scompt.

Com May 3 '10 at 15:27 Understandable, however, if you have the task at 5 hours then your thread will park for (5, TimeUnit. HOURS). Since its parked there would be no switching until it is unparked and the interruption will occur then.

– John Vint May 3 '10 at 15:29.

Wrap the task in FutureTask and you can specify timeout for the FutureTask. Look at the example in my answer to this question, stackoverflow.com/questions/1247390/java....

I realize there are a couple ways to do this using the java.util. Concurrent classes, but I'm looking for an ExecutorService implementation. – URL1 May 3 '10 at 14:56 If you are saying that you want your ExecutorService to hide the fact that timeouts are being added from client code, you could implement your own ExecutorService that wraps every runnable handed to it with a FutureTask before executing them.

– erikprice Sep 24 '10 at 15:23.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions