• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there an executor in java5 concurrency utilities which kills the task if timesout?

 
ying lam
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is there an executor in java5 concurrency utilities which kills the task if timesouts?

For example, I have an executor which executes task like this:


executor.execute( new Runnable() {
public void run() {
// do something, but it can take a long time
});

Is there a way for the executor to kill the task it the task takes longer than certain time to finish? Or I need to do something totally different?

Thank you.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use an invokeAll() method (in ExecutorService) that takes a timeout parameter:

The catch here is that the task in myCallable needs to be written so that it can respond to a thread interrupt if it times out. Otherwise the task will just keep executing.
 
ying lam
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

But why there is no invoke() in the ExecutorService?
What can I do if I just have 1 task, and I want it to finish within a certain time?

And I want it to be a ThreadPoolExecutor? So that I can reuse the thread in thread pool when a new task comes.

For example, I have a thread pool of 5 (non-growable), and i use thread pool executor to execute a task. What if all 5 tasks I assign to the executor never returns , it will result non of the new task get executed.

What I want to do it , after a certain time, if a task never returns, the exectuor kills the task. And the thread can be accepted for something else.

Thank you.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[YL]: But why there is no invoke() in the ExecutorService?

I guess they figured it wasn't really necessary, since it was just a special case of invokeAll(). It would have been nice, as a convenience, but it's not strictly necessary.

[YL]: What can I do if I just have 1 task, and I want it to finish within a certain time?

Um, that's the case that I showed code for. Do what I showed in my last post.

[YL]: And I want it to be a ThreadPoolExecutor? So that I can reuse the thread in thread pool when a new task comes.

That's fine - use a ThreadPoolExcecutor, which implements ExecutorService. In my last post I didn't make any assumptions about what kind of ExecutorService you're using. I would probably create the pool with Executors.newCachedThreadPool() or newFixedThreadPool(). But you can use whatever ExecutorService you like.

[YL]: For example, I have a thread pool of 5 (non-growable), and i use thread pool executor to execute a task. What if all 5 tasks I assign to the executor never returns , it will result non of the new task get executed.

Well, one option is to use a thread pool that can grow if necessary. And of course, by using timeouts you're increasing the chance that the threads will eventually complete. But that depends on how well the threads respond to interrupts.

[YL]: What I want to do it , after a certain time, if a task never returns, the exectuor kills the task. And the thread can be accepted for something else.

Yes, that's what we've been discussing, yes? The code I showed last time will interrupt the task after 1 minute if it hasn't completed. And if it interrupts successfully, the thread will then be available to the pool for other tasks.
[ January 27, 2008: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!