Skip to content

Instantly share code, notes, and snippets.

@lu911
Created August 29, 2013 06:30
Show Gist options
  • Save lu911/6374807 to your computer and use it in GitHub Desktop.
Save lu911/6374807 to your computer and use it in GitHub Desktop.
Actor Channel Example
/*
* 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