/bench/scala/oBigPingPong.scala

http://github.com/kilim/kilim · Scala · 82 lines · 61 code · 12 blank · 9 comment · 6 complexity · 721185ebc0c11f48d8d98b76b79572a8 MD5 · raw file

  1. package examples.actors
  2. /*
  3. Adapted from PingPongEx.scala, itself an adaption of an example
  4. from the scala distribution (added timing measurement).
  5. This example creates n tasks and has each send a message to the
  6. others. It sends the same structure (and number of messages) as the
  7. kilim/bench/BigPingPong example.
  8. */
  9. import scala.actors._
  10. import scala.actors.Actor._
  11. object BigPingPong {
  12. var beginTime: long = 0
  13. def main(args : Array[String]): Unit = {
  14. val nTasks = Integer.parseInt(args(0))
  15. var i = nTasks
  16. var tasks: Array[Ping] = new Array[Ping](nTasks)
  17. Scheduler.impl = new SingleThreadedScheduler
  18. val cTask = new Collector(nTasks);
  19. cTask.start()
  20. beginTime = System.currentTimeMillis()
  21. for (val i <- 0 to nTasks-1) {
  22. tasks(i) = new Ping(i)
  23. }
  24. for (val t <- tasks) {
  25. t.setOthers(tasks, cTask)
  26. t.start();
  27. }
  28. }
  29. }
  30. abstract class Msg
  31. case class PingMsg(from :int ) extends Msg
  32. class Ping(i: int) extends Actor {
  33. var id : int = i
  34. var tasks: Array[Ping] = Array()
  35. var ctask : Collector = null
  36. def setOthers(others: Array[Ping], ct : Collector) {
  37. ctask = ct
  38. tasks = others
  39. }
  40. def act(): unit = {
  41. System.out.println("" + id + " n = " + tasks.length);
  42. for (val t <- tasks) {
  43. if (t != this) {
  44. t ! new PingMsg(id)
  45. receive {
  46. case PingMsg(from) => {
  47. System.out.println ("" + from + " -> " + id);
  48. }
  49. }
  50. }
  51. }
  52. ctask ! new PingMsg(id)
  53. }
  54. }
  55. class Collector(n : int) extends Actor {
  56. val nTasks = n
  57. def act(): unit = {
  58. for (val i <- 1 to nTasks) {
  59. receive {
  60. case PingMsg(from) => {
  61. //System.out.println("Completed: " + from);
  62. }
  63. }
  64. }
  65. val elapsed = System.currentTimeMillis() - BigPingPong.beginTime;
  66. System.out.println("Elapsed : " + elapsed)
  67. System.exit(0);
  68. }
  69. }