How to abort a task in Scala?

As I understand it, Scala Actorsit is impossible to interrupt. Suppose now that I have a timeout task. If the task does not complete within the timeout, I must stop it.

Suppose a task is aborted (for example, it blocks I / O on an intermittent channel). In Java, I can run the task in a separate thread and interrupt the thread at timeout.

Can I do this with help Scala Actors? Should I use Java threads instead?

+5
source share
1 answer

You cannot interrupt an actor if you follow the actor model precisely. That is: if you do not want to share. This is suggested in How to cancel Akka's answer using AtomicBoolean, for example.

However, the general answer is this: you are trying to use Java thread naming in actors. This is not true. Instead of performing a long task, you should divide your work into smaller batches.

From Akka Documentation , Actor Best Practice:

: , . ( ) . (.. Thread) - , , , .. - ; . .

Scala ? , , , .

Java Threads ? Java, : . Actor concurrency, , .

+2

All Articles