import scala.actors.Actor import scala.actors.Actor._ import javax.swing.JOptionPane._ import scala.math._ case class Berechnen(f: ((Long, Long) => Long), x: Long, y: Long) class Prozess(val nr: Int, val f: (Long, Long) => Long, val prozessor: Prozessor) extends Actor { val min = 0; val max = 10; val delta = max - min; var wertetabelle = List.empty[(Long, Long, Any)] def act { for (i <- 1 to 10) { val x = min + (delta * random) round; val y = min + (delta * random) round val z = prozessor !! Berechnen(f, x, y) // Future - Nachricht wertetabelle = wertetabelle :+ (x,y,z()) println("Prozess " + nr + ": f("+x+","+y+") = " + z()); // <-- z ist Future! } exit // ... das Ende dieses Prozesses } } class Prozessor extends Actor { start def act { react { case Berechnen(f,x,y) => { Thread.sleep((50 * math.random).toLong); reply( f(x,y) ) } act // Rekursion } } } object Calculator extends Application { val functions = List( (x: Long, y: Long) => (x + y) * (x - y), (x: Long, y: Long) => x*x - y*y, (x: Long, y: Long) => x*x + y*y, (x: Long, y: Long) => x*y ) val prozessor = new Prozessor() var prozesse : Array[Prozess] = Array.ofDim(functions size) var i = 0; for (f <- functions) { prozesse(i) = new Prozess(i, f, prozessor); prozesse(i).start i += 1 } Thread.sleep(1000) var out = "" for (p <- prozesse) { out += p.wertetabelle.mkString + "\n" } showMessageDialog(null, out, "Wertetabellen", -1) System.exit(0) // der Prozessor wartet ja immer noch ... }