ExecutorCompletionService for scheduled task

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

ExecutorCompletionService for scheduled task

Hanson Char
Hi,

I am not sure if the existing ExecutorCompletionService can be nicely
fitted here.  What we want is a scheduled close operation to be
initiated on each open session after m seconds delay, and abort
regardless after a total of n seconds, even if some of these close
operations haven't started or finished, where n > m.

ExecutorCompletionService.submit() allows some tasks to be submitted
for execution, but there doesn't exist something like
ExecutorCompletionService.schedule() that would take a delay
parameter.

Thoughts ?

Hanson Char
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: ExecutorCompletionService for scheduled task

Dhanji R. Prasanna
Have you looked into using ScheduledThreadPoolExecutor with CompletionService?
The schedule() method takes a delay which can be used to trigger the
close operation.

Not sure if this is exactly what you want to do?

On 8/13/06, Hanson Char <[hidden email]> wrote:

> Hi,
>
> I am not sure if the existing ExecutorCompletionService can be nicely
> fitted here.  What we want is a scheduled close operation to be
> initiated on each open session after m seconds delay, and abort
> regardless after a total of n seconds, even if some of these close
> operations haven't started or finished, where n > m.
>
> ExecutorCompletionService.submit() allows some tasks to be submitted
> for execution, but there doesn't exist something like
> ExecutorCompletionService.schedule() that would take a delay
> parameter.
>
> Thoughts ?
>
> Hanson Char
> _______________________________________________
> Concurrency-interest mailing list
> [hidden email]
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: ExecutorCompletionService for scheduled task

Hanson Char
That's what I am using.  Specifically:

 Executors.newScheduledThreadPool(...);

plus other code to schedule via the ScheduledExecutorService, and then
to wait for timeout via the ScheduledFuture.get(...)

Just wonder if there is a better (lazier) way.

Hanson

On 8/13/06, Dhanji R. Prasanna <[hidden email]> wrote:

> Have you looked into using ScheduledThreadPoolExecutor with CompletionService?
> The schedule() method takes a delay which can be used to trigger the
> close operation.
>
> Not sure if this is exactly what you want to do?
>
> On 8/13/06, Hanson Char <[hidden email]> wrote:
> > Hi,
> >
> > I am not sure if the existing ExecutorCompletionService can be nicely
> > fitted here.  What we want is a scheduled close operation to be
> > initiated on each open session after m seconds delay, and abort
> > regardless after a total of n seconds, even if some of these close
> > operations haven't started or finished, where n > m.
> >
> > ExecutorCompletionService.submit() allows some tasks to be submitted
> > for execution, but there doesn't exist something like
> > ExecutorCompletionService.schedule() that would take a delay
> > parameter.
> >
> > Thoughts ?
> >
> > Hanson Char
> > _______________________________________________
> > Concurrency-interest mailing list
> > [hidden email]
> > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
> >
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: ExecutorCompletionService for scheduled task

Joe Bowbeer
Have you thought about scheduling additional tasks whose job it is to
cancel the previously submitted tasks (and eventually close their
resources)?

For each task you schedule on the completion service, I'm imagining
you would schedule another task (or two) on a "cancellation service".
If a task completes in time, you would cancel its associated
cancellation task(s).  Otherwise, the cancellation task will cancel
its associated task and you'll dequeue that failed task from the
completion service.

If timing is a bit more complicated, each task may need to schedule
its own cancellation when it runs.  This is harder to do, I think,
because some of the wrapping added by the scheduled executor and/or
the completion service.


On 8/13/06, Hanson Char <[hidden email]> wrote:

> That's what I am using.  Specifically:
>
>  Executors.newScheduledThreadPool(...);
>
> plus other code to schedule via the ScheduledExecutorService, and then
> to wait for timeout via the ScheduledFuture.get(...)
>
> Just wonder if there is a better (lazier) way.
>
> Hanson
>
> On 8/13/06, Dhanji R. Prasanna <[hidden email]> wrote:
> > Have you looked into using ScheduledThreadPoolExecutor with CompletionService?
> > The schedule() method takes a delay which can be used to trigger the
> > close operation.
> >
> > Not sure if this is exactly what you want to do?
> >
> > On 8/13/06, Hanson Char <[hidden email]> wrote:
> > > Hi,
> > >
> > > I am not sure if the existing ExecutorCompletionService can be nicely
> > > fitted here.  What we want is a scheduled close operation to be
> > > initiated on each open session after m seconds delay, and abort
> > > regardless after a total of n seconds, even if some of these close
> > > operations haven't started or finished, where n > m.
> > >
> > > ExecutorCompletionService.submit() allows some tasks to be submitted
> > > for execution, but there doesn't exist something like
> > > ExecutorCompletionService.schedule() that would take a delay
> > > parameter.
> > >
> > > Thoughts ?
> > >
> > > Hanson Char
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: ExecutorCompletionService for scheduled task

Dhanji R. Prasanna
In reply to this post by Hanson Char
On 8/14/06, Hanson Char <[hidden email]> wrote:
> plus other code to schedule via the ScheduledExecutorService, and then
> to wait for timeout via the ScheduledFuture.get(...)
>

I think that's about the simplest/most practical solution so long as
you are happy that the thread calling SF.get() is reliably early and
isolated (i.e. is able to call SF.get() on all scheduled threads
independently). If not, I think Joe's idea of using a parallel watcher
is the better solution.

> Just wonder if there is a better (lazier) way.
>
> Hanson
>
> On 8/13/06, Dhanji R. Prasanna <[hidden email]> wrote:
> > Have you looked into using ScheduledThreadPoolExecutor with CompletionService?
> > The schedule() method takes a delay which can be used to trigger the
> > close operation.
> >
> > Not sure if this is exactly what you want to do?
> >
> > On 8/13/06, Hanson Char <[hidden email]> wrote:
> > > Hi,
> > >
> > > I am not sure if the existing ExecutorCompletionService can be nicely
> > > fitted here.  What we want is a scheduled close operation to be
> > > initiated on each open session after m seconds delay, and abort
> > > regardless after a total of n seconds, even if some of these close
> > > operations haven't started or finished, where n > m.
> > >
> > > ExecutorCompletionService.submit() allows some tasks to be submitted
> > > for execution, but there doesn't exist something like
> > > ExecutorCompletionService.schedule() that would take a delay
> > > parameter.
> > >
> > > Thoughts ?
> > >
> > > Hanson Char
> > > _______________________________________________
> > > Concurrency-interest mailing list
> > > [hidden email]
> > > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
> > >
> >
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: ExecutorCompletionService for scheduled task

Hanson Char
Below is the "simplest" or most "lightweight" code fragment I can
think of to cancel all tasks (that have been scheduled to run after M
seconds delay) if not all completed within a total of N seconds (where
N > M).  Please shout if you spot any weakness/drawback.

Hanson

...
        List<ScheduledFuture> scheduledFutureList = ...// scheduled to
run after M seconds delay

        // Let's wait for a maximum of N seconds.
        // We will exit regardless even if not all open sessions have
been closed.
        final long dueTimeMillis = System.currentTimeMillis() + N * 1000;
        boolean interrupted = false;

        for (ScheduledFuture scheduledFuture : scheduledFutureList)
        {
            long timeoutMillis = dueTimeMillis - System.currentTimeMillis();

            if (timeoutMillis <= 0)
            {
                scheduledFuture.cancel(true);
                continue;
            }
            try {
                scheduledFuture.get(timeoutMillis, TimeUnit.MILLISECONDS);
            } catch (ExecutionException e) {
               // log it or whatever
            } catch (TimeoutException e) {
               // log it or whatever
            } catch (InterruptedException e) {
               // log it or whatever
                interrupted = true;
            } finally {
                scheduledFuture.cancel(true);
            }
        }

        if (interrupted)
            Thread.currentThread().interrupt();

...


On 8/13/06, Dhanji R. Prasanna <[hidden email]> wrote:

> On 8/14/06, Hanson Char <[hidden email]> wrote:
> > plus other code to schedule via the ScheduledExecutorService, and then
> > to wait for timeout via the ScheduledFuture.get(...)
> >
>
> I think that's about the simplest/most practical solution so long as
> you are happy that the thread calling SF.get() is reliably early and
> isolated (i.e. is able to call SF.get() on all scheduled threads
> independently). If not, I think Joe's idea of using a parallel watcher
> is the better solution.
>
> > Just wonder if there is a better (lazier) way.
> >
> > Hanson
> >
> > On 8/13/06, Dhanji R. Prasanna <[hidden email]> wrote:
> > > Have you looked into using ScheduledThreadPoolExecutor with CompletionService?
> > > The schedule() method takes a delay which can be used to trigger the
> > > close operation.
> > >
> > > Not sure if this is exactly what you want to do?
> > >
> > > On 8/13/06, Hanson Char <[hidden email]> wrote:
> > > > Hi,
> > > >
> > > > I am not sure if the existing ExecutorCompletionService can be nicely
> > > > fitted here.  What we want is a scheduled close operation to be
> > > > initiated on each open session after m seconds delay, and abort
> > > > regardless after a total of n seconds, even if some of these close
> > > > operations haven't started or finished, where n > m.
> > > >
> > > > ExecutorCompletionService.submit() allows some tasks to be submitted
> > > > for execution, but there doesn't exist something like
> > > > ExecutorCompletionService.schedule() that would take a delay
> > > > parameter.
> > > >
> > > > Thoughts ?
> > > >
> > > > Hanson Char
> > > > _______________________________________________
> > > > Concurrency-interest mailing list
> > > > [hidden email]
> > > > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
> > > >
> > >
> >
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest