Question

I am currently modifying an application to use a ScheduledExecutorService in place of a Timer and I used to access the scheduled task with a Map I kept, and it allowed me to cancel() the task as well as accessing it.

With this API it seems that I have to maintain two Maps, one for accessing the tasks and one for the SheduledFuture<?>s returned by schedule() to be able to cancel them.

I read this post but it looks very heavy to implement compared to what I did with a Timer (Only one Map was needed).

This is a simple application that has two tasks that need to be accessed and cancelled.

Is there something I didn't get or are `Executor's not what I need for something this simple?

Was it helpful?

Solution

So you want following:

  1. You want the instance of task submitted as it has some info.
  2. You need ability to cancel the submitted task.

Modify Class definition of your submitted task to have an instance of Future in it. Now when you submit your task to ExecutorService it will return a Future , you can set this Future object in your submitted task object. So now you just need to retain the submitted task and you will info as well as ability to cancel task via future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top