Created
August 29, 2013 06:30
-
-
Save lu911/6374807 to your computer and use it in GitHub Desktop.
Actor Channel Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright © 2013 Yuk SeungChan, All rights reserved. | |
*/ | |
package main.scala | |
import scala.actors.{Channel, OutputChannel, Actor} | |
object Main { | |
def main(args: Array[String]): Unit = { | |
val channel = new Channel[Int] | |
val computeActor: Computer = new Computer | |
computeActor.start() | |
val input: Seq[Int] = Array[Int](1,2,3) | |
computeActor ! Compute(input, channel) | |
channel.receive { | |
case x => println(x) | |
case _ => println("None") | |
} | |
} | |
} | |
case class Compute(input: Seq[Int], result: OutputChannel[Int]) | |
class Computer extends Actor { | |
def act(){ | |
while(true) | |
{ | |
receive { | |
case Compute(input, output) => { | |
output ! (input.reduce(_*_)) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment