How to use Callable instead of Runnable with ScheduledThreadPoolExecutor?

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

How to use Callable instead of Runnable with ScheduledThreadPoolExecutor?

syg6
Hello concurrent people,

I have been futzing around with the Java 1.5 concurrent classes for a while now and more or less have the hang of things. But there is one thing I can't figure out. I'd like to schedule a task to execute every x seconds, have it return me a list of Futures for those tasks and, based on the result in each Future, do something.

I'd like to use either scheduleAtFixedRate() or scheduleWithFixedDelay() but both of these take Runnable as their first parameter, and I'd like to use a Callable (to get a Result). I see that schedule() and submit() both take Callable as a parameter but that's for a one-shot execution, I need something periodic.

Any way to do this?

Many thanks guys,
Bob

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

Re: How to use Callable instead of Runnable with ScheduledThreadPoolExecutor?

tpeierls
It's hard to see how to make this work. Would you expect to receive the list of Futures upon submission of the periodic task? In what order would you wait for the results?

Instead, why not use a callback style?

interface PeriodicTaskListener<T> {
    void resultReturned(T result);
    void exceptionThrown(Exception exception);
}

class PeriodicTask<T> implements Runnable {
    private final Callable<T> callable;
    private final PeriodicTaskListener<T> listener;

    PeriodicTask(Callable<T> callable,
                 PeriodicTaskListener<T> listener) {

        this.callable = callable;
        this.listener = listener;
    }

    public void run() {
        try {
            listener.resultReturned(callable.call());
        } catch (Exception e) {
            listener.exceptionThrown(e);
        }
    }
}

Then you can use it like this:

scheduledThreadPool.scheduleAtFixedRate(new PeriodicTask<Long>(
    new Callable<Long>() {
        public Long call() throws Exception {
            ... compute some value ...
        }

    },
    new PeriodicTaskListener<Long>() {
        public void resultReturned(Long result) {
            ... do something with result ...
        }

        public void exceptionThrown(Exception ex) {
            ... do something about ex ...
        }

    },
    5L, 5L, TimeUnit.SECONDS // every five seconds
));

--tim

On 10/2/06, Robert Bowen <[hidden email]> wrote:
Hello concurrent people,

I have been futzing around with the Java 1.5 concurrent classes for a while now and more or less have the hang of things. But there is one thing I can't figure out. I'd like to schedule a task to execute every x seconds, have it return me a list of Futures for those tasks and, based on the result in each Future, do something.

I'd like to use either scheduleAtFixedRate() or scheduleWithFixedDelay() but both of these take Runnable as their first parameter, and I'd like to use a Callable (to get a Result). I see that schedule() and submit() both take Callable as a parameter but that's for a one-shot execution, I need something periodic.

Any way to do this?

Many thanks guys,
Bob

_______________________________________________
Concurrency-interest mailing list
[hidden email]
<a onclick="return top.js.OpenExtLink(window,event,this)" href="http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest" target="_blank">http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest




_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest