红联Linux门户
Linux帮助

Understanding actor concurrency, Part 2: Actors on the JVM

发布时间:2009-03-31 14:16:40来源:红联作者:jerry520
In the first half of his introduction to actorconcurrency, Alex Miller discussed the limitations of shared-stateconcurrency and explained how the actor model is expressed in Erlang.While Erlang is a nonstarter for many shops, actor implementations doexist for languages that run on the JVM. Find out how actors work andsee them implemented using Scala's standard library, Groovy'sGParallelizer, and the Java libraries Kilim, ActorFoundry, ActorsGuild, and Jetlang.
In my previous articleI introduced you to the advantages of the actor concurrency model overthe shared-stated model used by popular imperative languages like Java.To help you understand actor concurrency, I explained how it'simplemented in Erlang, a functional language that has been around for acouple of decades. Erlang's implementation is elegant, but it's anonstarter if you're committed to developing applications in a languagethat runs on the JVM. So, in this article I'll look at Erlang-inspiredactor 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.
Listing 1. Actor libraries on the JVM [table=40%,#ffffcc] [tr] [td]Language[/td] [td]Library[/td] [td]Weaving strategy[/td] [/tr] [tr] [td]Scala 2.7.3[/td] [td]Scala API[/td] [td]n/a[/td] [/tr] [tr] [td]Groovy 1.6[/td] [td]GParallelizer 0.6[/td] [td]n/a[/td] [/tr] [tr] [td]Java[/td] [td]Kilim 0.6[/td] [td]compile-time[/td] [/tr] [tr] [td]Java[/td] [td]ActorFoundry 1.0[/td] [td]compile-time[/td] [/tr] [tr] [td]Java[/td] [td]Actors Guild 0.6[/td] [td]runtime[/td] [/tr] [tr] [td]Java[/td] [td]Jetlang 0.1.7[/td] [td]n/a[/td] [/tr] [/table] 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 codeanytime.) Each implementation uses two Player actors and a Coordinatoractor. The Coordinator requests each player to play, and each Playerreplies with Rock, Paper, or Scissors. The Coordinator accepts theresponses, announces the winner, and starts over. The message flow isillustrated in Figure 1.
[img=351,229]http://www.javaworld.com/javaworld/jw-03-2009/images/actors2-fig1.jpg[/img]
Figure 1. Rock-Paper-Scissors message flow Actors in Scala Scala features the most faithful implementation of the Erlang actor model on the JVM and has made it a central part of Scala's concurrencystory. Scala is a hybrid language that takes cues from both theobject-oriented and functional programming traditions. It is designedto be extensible and to grow into new language features as needed. Thelanguage itself is built on a small core of principles, but it has amuch larger perceived surface area thanks to these "scalable" languagecapabilities.
In fact, the Scalaactors implementation is provided as part of the Scala standard library(equivalent to the JDK in Java), not as part of the language. Itreplicates much of the Erlang actor model. The fact that this ispossible 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 traitin the Scala standard library. In Scala, a trait is a set of method andfield definitions that can be mixed into other classes. This is similarto providing an abstract superclass in Java but more flexible,particularly in comparison to multiple inheritance.
文章评论

共有 0 条评论