标签:style blog http io ar color sp for java
手动敲了一遍计算pi的示例:http://www.gtan.com/akka_doc/intro/getting-started-first-scala.html
有个笔误,花了半个小时定位。
1 [Torstani@sparkb5-i ~/akka_example/pi]$ cat src/Pi.scala 2 package akka.tutorial.first.scala 3 import akka.actor._ 4 import akka.routing.RoundRobinRouter 5 import scala.concurrent.duration._ 6 7 object Pi extends App{ 8 calculate(nrOfWorkers=4, nrOfElements=1000, nrOfMessages=10000) 9 sealed trait PiMessage 10 case object Calculate extends PiMessage 11 case class Work(stat:Int, nrOfElements: Int) extends PiMessage 12 case class Result(value: Double) extends PiMessage 13 case class PiApproximation(pi: Double, duration: Duration) 14 15 class Worker extends Actor{ 16 def calculatePiFor(start: Int, nrOfElements: Int):Double = { 17 var acc = 0.0 18 for(i<- start until (start + nrOfElements)) 19 acc += 4.0 * (1 - (i%2) * 2) / (2 * i + 1) 20 acc 21 } 22 def receive = { 23 case Work(start, nrOfElements) => 24 //println("worker received "+start + ":"+nrOfElements) 25 val res = calculatePiFor(start, nrOfElements) 26 sender ! Result(res) 27 //println("worker send result: "+res) 28 } 29 } 30 31 class Master(nrOfWorkers:Int, nrOfMessages:Int, nrOfElements: Int, listener: ActorRef) extends Actor { 32 var pi:Double = _ 33 var nrOfResults:Int= _ 34 val start:Long = System.currentTimeMillis 35 36 val workerRouter = context.actorOf( 37 Props[Worker].withRouter(RoundRobinRouter(nrOfWorkers)), name="workerRouter") 38 def receive = { 39 case Calculate => 40 //println("master receive Calculate") 41 for(i <- 0 until nrOfMessages) 42 workerRouter ! Work(i*nrOfElements, nrOfElements) 43 case Result(value) => 44 //println("master recieve result: "+value) 45 pi += value 46 nrOfResults += 1 47 if(nrOfResults == nrOfMessages){ 48 listener ! PiApproximation(pi, duration=(System.currentTimeMillis - start).millis) 49 context.stop(self) 50 } 51 } 52 } 53 class Listener extends Actor{ 54 def receive = { 55 case PiApproximation(pi, duration) => 56 println("\tpi approximation: \t\t%s\n\tCalculation time: \t%s".format(pi, duration)) 57 context.system.shutdown() 58 } 59 } 60 def calculate(nrOfWorkers:Int, nrOfElements:Int, nrOfMessages:Int) = { 61 val system = ActorSystem("PiSystem") 62 val listener = system.actorOf(Props[Listener], name="listener") 63 val master = system.actorOf(Props(new Master(nrOfWorkers, nrOfMessages, nrOfElements, listener)), name="master") 64 master ! Calculate 65 } 66 }
1 [Torstan@sparkb5-i ~/akka_example/pi]$ cat Makefile 2 SRC_DIR := src 3 SRC := $(shell find ${SRC_DIR} -name "*.scala") 4 DIR=remote 5 6 TARGET := HelloRemote.jar 7 8 SCALAC := scalac 9 SCFLAGS := /usr/local/scala-2.10.4/lib/akka-actors.jar 10 11 .PHONY: all clean 12 13 all: ${TARGET} 14 15 ${TARGET}: ${SRC} 16 ${SCALAC} -cp ${SCFLAGS} $^ 17 18 clean: 19 ${RM} -r ${TARGET} ${DIR}
1 [Torstan@sparkb5-i ~/akka_example/pi]$ cat run.sh 2 #!/bin/bash 3 4 AKKA_LIB_PATH="/usr/local/akka-2.1.4/lib/akka/" 5 6 java -cp 7 .:/usr/local/scala-2.10.4/lib/scala-library.jar:/usr/local/scala-2.10.4/lib/akka-actors.jar:/usr/local/scala-2.10.4/lib/typesafe-config.jar:${AKKA_LIB_PATH}/akka-remote_2.10-2.1.4.jar:${AKKA_LIB_PATH}/protobuf-java-2.4.1.jar:${AKKA_LIB_PATH}/netty-3.5.8.Final.jar 8 akka.tutorial.first.scala.Pi
执行结果:
[Torstan@sparkb5-i ~/akka_example/pi]$ ./run.sh
pi approximation: 3.1415925535897866
Calculation time: 383 milliseconds
标签:style blog http io ar color sp for java
原文地址:http://www.cnblogs.com/Torstan/p/4156440.html