Working with Multiple Actors

We now know how to create an actor and send messages to it. Let’s get a feel for putting multiple actors to work. In Chapter 2, Division of Labor, we created a concurrent program to count primes in a range. In the example in Concurrent Computation of Prime Numbers, we used ExecutorService, Callable, and Future and filled up a little over a page with code. Let’s see how that example shapes up with Akka actors first in Java and then in Scala.

Multiple Actors in Java

Given a number like 10 million, we divided the computation of primes into different ranges and distributed these ranges over several threads. Here we’re going to use actors. Let’s start with the actor’s onReceive method:

favoringIsolatedMutability/java/primes/Primes.java ...

Get Programming Concurrency on the JVM now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.