This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes ScheduledExecutorService - Time To Live 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 » Java in General
Bookmark "ScheduledExecutorService - Time To Live" Watch "ScheduledExecutorService - Time To Live" New topic
Author

ScheduledExecutorService - Time To Live

Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3700
    
    5

I've been working with ScheduledExecutorService.scheduleAtFixedRate() and noticed is has some odd behaviors related to incomplete executions. For example, if an Exception is thrown that is not caught within the Runnable method, all future executions of the schedule will be cancelled. I also noticed if a thread takes deadlocks or "just takes a really long time", all future executions will delayed indefinitely.

Is there a way to set a time to live when using the ScheduledExecutorService service? My goal: Have a process that runs every 60 seconds regardless of whether the previous process failed or is still running. Alternatively, it could kill the previous process if it is still running after some specified time to live.

I can fix the exception case by wrapping the content of the run method with a catch block for Throwable, but I'm not sure how to set a thread time to live and/or allow the thread to start while a previous instance is still running.


My Blog: Down Home Country Coding with Scott Selikoff
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3415
    
  12
Rather than using the scheduleAtFixedRate method , could you use the scheduleWithFixedDelay method to trigger the first one and then call it again as the first thing you do in your Runnable's run method ?


Joanne
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3700
    
    5

There's really no difference between on how these two function other than the time the subsequent thread starts. If one thread deadlocks or dies, all subsequent passes will be cancelled regardless of the method used.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4338
    
    7

Could you use the scheduler to kick off yet another thread each time to do the processing in? That way the ScheduledExecutorService would be (I would expect) completely unaware of the success or failure of the processing itself.
(Yes, I realise that's ugly, and there must be a better way, but it might just be a way of making a small change to get it working.)
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

You can probably have a reaper thread that checks whether your task is still running, if yes, then call a cancel on the ScheduledFuture you got out of the scheduledAtFixedRate() call. On successful cancel you can re-schedule another task with zero initial delay.
You can track whether a task is done or not (ScheduledFuture#isDone() will not give you status of intermediate executions) by tracking execution using beforeExecute() and afterExecute(). There will be some race conditions here that you will have to handle though.

Of course, this solution mandates that your task responds to interrupts, if not, then anyways there is very little you can do safely.

apigee, a better way to API!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ScheduledExecutorService - Time To Live
 
Similar Threads
Query about ScheduledThreadPoolExecutor.scheduleAtFixedRate
Best Option To Buffer Mails?
Cancelling a thread/task
Util library to help make my code run *slower*?
ExecutorService and FutureTask - Blocked Thread