Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
In the first half of his introduction to actor concurrency, Alex Miller discussed the limitations of shared-state concurrency and explained how the actor model is expressed in Erlang. While Erlang is a nonstarter for many shops, actor implementations do exist for languages that run on the JVM. Find out how actors work and see them implemented using Scala's standard library, Groovy's GParallelizer, and the Java libraries Kilim, ActorFoundry, Actors Guild, and Jetlang.
In my previous article I introduced you to the advantages of the actor concurrency model over the shared-stated model used by popular imperative languages like Java. To help you understand actor concurrency, I explained how it's implemented in Erlang, a functional language that has been around for a couple of decades. Erlang's implementation is elegant, but it's a nonstarter if you're committed to developing applications in a language that runs on the JVM. So, in this article I'll look at Erlang-inspired actor implementations in three JVM languages: Scala, Groovy, and Java.
Table 1 is an overview of the languages and actor libraries I'll discuss in this article.
|Scala 2.7.3||Scala API||n/a|
|Groovy 1.6||GParallelizer 0.6||n/a|
|Java||Actors Guild 0.6||runtime|
I'll use the schoolyard game Rock-Paper-Scissors as a reference application and implement it in each library to demonstrate the features and differences. (You can download the source code anytime.) Each implementation uses two Player actors and a Coordinator actor. The Coordinator requests each player to play, and each Player replies with Rock, Paper, or Scissors. The Coordinator accepts the responses, announces the winner, and starts over. The message flow is illustrated in Figure 1.
Scala features the most faithful implementation of the Erlang actor model on the JVM and has made it a central part of Scala's concurrency story. Scala is a hybrid language that takes cues from both the object-oriented and functional programming traditions. It is designed to be extensible and to grow into new language features as needed. The language itself is built on a small core of principles, but it has a much larger perceived surface area thanks to these "scalable" language capabilities.
In fact, the Scala actors implementation is provided as part of the Scala standard library (equivalent to the JDK in Java), not as part of the language. It replicates much of the Erlang actor model. The fact that this is possible to do well as a library is a testament to Scala's flexibility.
Scala actors support all of the key concepts from Erlang. Actors are defined as a trait in the Scala standard library. In Scala, a trait is a set of method and field definitions that can be mixed into other classes. This is similar to providing an abstract superclass in Java but more flexible, particularly in comparison to multiple inheritance.