concurrency - When could Futures be more appropriate than Actors (or vice versa) in Scala? -
suppose need run few concurrent tasks.
i can wrap each task in future
, wait completion. alternatively can create actor
each task. each actor
execute task (e.g. upon receiving "start" message) , send result back.
i wonder when should use former (with future
s) , latter (with actor
s) approach , why future
approach considered better case described above.
because syntactically simpler.
val tasks: seq[() => t] = ??? val futures = tasks map { t => future { t() } } val results: future[seq[t]] = future.sequence(futures)
the results
future can wait on using await.result
or can map further/use in for-comprehension or install callbacks on it.
compare instantiating actors, sending messages them, coding receive
blocks, receiving responses them , shutting them down -- require more boilerplate.
Comments
Post a Comment