Migrating from Java concurrency to Scala concurrency

☆樱花仙子☆ 提交于 2019-12-03 14:36:31

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!