I have a fairly standard mechanism in Java for solving the problem:
- Work items must be scheduled to execute at a particular time
- Each work item must then wait on a condition becoming true
- Work items should be cancellable
The solution I use is as follows:
- Have a single-threaded scheduler to schedule my work items
- Have an
ExecutorService
(which may be multi-threaded) - Each scheduled work item then submits the actual work to the
ExecutorService
. The returnedFuture
is cached in a map. A completion service is used to remove the future from the cache when the work is completed - Items can be cancelled via the cached futures
Of course, my executor needs to be at least as big as the number of blocking work items I expect to have but this is not a problem in practice.
So now I'm coding in Scala and using the actor framework. Assuming that my work item can be encapsulated in an event sent to an actor:
- What mechanism would I use to schedule a work item for a specific time?
- If a work item is an event sent to an actor, how can I ensure that the backing thread pool is bigger than the number of items that can be blocking at the same time
- How can I cause a previously scheduled work item to be cancelled?