I have a fairly standard mechanism in Java for solving the problem:
The solution I use is as follows:
ExecutorService
(which may be multi-threaded)ExecutorService
. The returned Future
is cached in a map. A completion service is used to remove the future from the cache when the work is completedOf 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?
I would use a java.util.concurrent.ScheduledExecutorService.
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
This strikes me as a design that defeats the effort of parallelisation. Try to minimise or eliminate blocking and global state. These are barriers to composability and scalability. For example, consider having a single dedicated thread that waits for files to arrive and then fires events off to actors. Or look at java.nio for asynchronous non-blocking I/O.
I don't fully understand your requirements here, but it seems like you could have a single thread/actor looking for I/O events. Then as your scheduled "work items", schedule effects that create non-blocking actors. Have those actors register themselves with the I/O thread/actor to receive messages about I/O events that they care about.
How can I cause a previously scheduled work item to be cancelled?
ScheduledExecutorService
returns Futures. What you have is not a bad design in that regard. Collect them in a Map and call future.cancel().
You could have a scheduling actor that has a list of scheduled actors, and uses Actor.receiveWithin() to wake up every second or so and send messages to actors that are ready to be executed. The scheduling actor could also handle cancelling. Another option is to let every actor handle its own scheduling directly with receiveWithin(), instead of centralizing scheduling.
There is some discussion on this issue in the blog post Simple cron like scheduler in Scala.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With