O'Reilly logo

Programming Concurrency on the JVM by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required