views:

222

answers:

1

I'm an 18 years old trainee and I'm discovering scala which I like very much :-). To get familiar with the scala actors I wrote a small simulation with some gears and one controller. The ActorApplication creates N gears with random speed. The controller calculates a synchronization speed and starts the gear-actors. The gears synchronize to this given speed step by step (1+ or 1-). The simulation is finished when all gears have reached the synchronization speed. I developed the simulation in scala 2.7.7 - and it worked as I expected it (see output below). However, when I swichted to the current 2.8.0-SNAPSHOT I discovered strange behavior. This is the code:

/**
 * Created by IntelliJ IDEA.
 * User: pmei
 */
import actors.Actor
import actors.Actor._
import collection.mutable.ListBuffer
import java.util.Random

case class ReceivedSpeed(gear: Gear)
case class StartSync

case class SyncGear(controller: GearController, syncSpeed: Int)

object ActorApplication extends Application {
  private val random = new Random() //scala.util random won't be random :(

  println("[App] start with creating gears")
  val gearList = new ListBuffer[Gear]()
  for (i <- 0 until 100) {
    gearList += new Gear(i)
  }

  val gearController = new GearController(gearList)

  gearController.start()
  gearController ! StartSync()
}

/**
 * CONTROLLER
 */
class GearController(nGears: ListBuffer[Gear]) extends Actor {

  private var syncGears = new ListBuffer[Gear]
  private var syncSpeed = 0
  def act = {
    loop {
      react {
        case StartSync() => {
          println("[Controller] Send commands for syncing to gears!")
          var speeds = new ListBuffer[Int]
          nGears.foreach(e => speeds += e.speed)

          //Calc avg
          //var avgSpeed = speeds.foldLeft(0)(_ + _) / speeds.length
          //var avgSpeed = speeds.foldLeft(0) { (x, y) => x + y } / speeds.length
          syncSpeed = (0/:speeds)(_ + _) / speeds.length //Average over all gear speeds

          println("[Controller] calculated syncSpeed: "+syncSpeed)
          nGears.foreach{e =>
                         e.start()
                         e ! SyncGear(this, syncSpeed)
          }
          println("[Controller] started all gears")
        }
        case ReceivedSpeed(gear: Gear) => {
          println("[Controller] Syncspeed received by a gear ("+gear.gearId+")")
          //println("[Controller] mailboxsize: "+self.mailboxSize)
          syncGears += gear
          if(syncGears.length == nGears.length) {
            println("[Controller] all gears are back in town!")
            System.exit(0)
          }
        }
        case _ => println("[Controller] No match :(")
      }
    }
  }
}

/**
 * GEAR
 */
class Gear(id: Int) extends Actor {

  private val random = new Random() //scala.util random won't be random :(
  private var mySpeed = random.nextInt(1000)
  private var myController: GearController = null

  def speed = mySpeed
  def gearId = id

  println("[Gear ("+id+")] created with speed: "+mySpeed)

  def act = {
    loop {
      react {
        case SyncGear(controller: GearController, syncSpeed: Int) => {
          //println("[Gear ("+id+")] activated, try to follow controller command (form mySpeed ("+mySpeed+") to syncspeed ("+syncSpeed+")")
          myController = controller
          adjustSpeedTo(syncSpeed)
        }
      }
    }
  }

  def adjustSpeedTo(targetSpeed: Int) = {
    if(targetSpeed > mySpeed) {
      mySpeed += 1
      self ! SyncGear(myController, targetSpeed)
    }else if(targetSpeed < mySpeed) {
      mySpeed -= 1
      self ! SyncGear(myController, targetSpeed)
    } else if(targetSpeed == mySpeed) {
      callController
    }
  }

  def callController = {
    println("[Gear ("+id+")] has syncSpeed")
    myController ! ReceivedSpeed(this)
  }
}

The code above generates in scala 2.7.7 an output like:

[App] start with creating gears
[Gear (0)] created with speed: 935
[Gear (1)] created with speed: 398
[Gear (2)] created with speed: 164
[Gear (3)] created with speed: 64
[Gear (4)] created with speed: 461
[Gear (5)] created with speed: 741
[Gear (6)] created with speed: 93
[Gear (7)] created with speed: 850
[Gear (8)] created with speed: 679
[Gear (9)] created with speed: 587
[Gear (10)] created with speed: 789
[Gear (11)] created with speed: 849
[Gear (12)] created with speed: 386
[Gear (13)] created with speed: 981
[Gear (14)] created with speed: 246
[Gear (15)] created with speed: 661
[Gear (16)] created with speed: 245
[Gear (17)] created with speed: 655
[Gear (18)] created with speed: 193
[Gear (19)] created with speed: 240
[Gear (20)] created with speed: 72
[Gear (21)] created with speed: 884
[Gear (22)] created with speed: 334
[Gear (23)] created with speed: 169
[Gear (24)] created with speed: 665
[Gear (25)] created with speed: 773
[Gear (26)] created with speed: 333
[Gear (27)] created with speed: 524
[Gear (28)] created with speed: 673
[Gear (29)] created with speed: 189
[Gear (30)] created with speed: 573
[Gear (31)] created with speed: 762
[Gear (32)] created with speed: 162
[Gear (33)] created with speed: 735
[Gear (34)] created with speed: 222
[Gear (35)] created with speed: 854
[Gear (36)] created with speed: 124
[Gear (37)] created with speed: 650
[Gear (38)] created with speed: 514
[Gear (39)] created with speed: 0
[Gear (40)] created with speed: 106
[Gear (41)] created with speed: 117
[Gear (42)] created with speed: 912
[Gear (43)] created with speed: 782
[Gear (44)] created with speed: 161
[Gear (45)] created with speed: 748
[Gear (46)] created with speed: 311
[Gear (47)] created with speed: 168
[Gear (48)] created with speed: 776
[Gear (49)] created with speed: 424
[Gear (50)] created with speed: 749
[Gear (51)] created with speed: 819
[Gear (52)] created with speed: 664
[Gear (53)] created with speed: 996
[Gear (54)] created with speed: 877
[Gear (55)] created with speed: 808
[Gear (56)] created with speed: 781
[Gear (57)] created with speed: 464
[Gear (58)] created with speed: 408
[Gear (59)] created with speed: 337
[Gear (60)] created with speed: 822
[Gear (61)] created with speed: 812
[Gear (62)] created with speed: 979
[Gear (63)] created with speed: 290
[Gear (64)] created with speed: 972
[Gear (65)] created with speed: 20
[Gear (66)] created with speed: 787
[Gear (67)] created with speed: 680
[Gear (68)] created with speed: 466
[Gear (69)] created with speed: 554
[Gear (70)] created with speed: 53
[Gear (71)] created with speed: 212
[Gear (72)] created with speed: 90
[Gear (73)] created with speed: 535
[Gear (74)] created with speed: 268
[Gear (75)] created with speed: 893
[Gear (76)] created with speed: 847
[Gear (77)] created with speed: 671
[Gear (78)] created with speed: 106
[Gear (79)] created with speed: 779
[Gear (80)] created with speed: 433
[Gear (81)] created with speed: 757
[Gear (82)] created with speed: 400
[Gear (83)] created with speed: 88
[Gear (84)] created with speed: 89
[Gear (85)] created with speed: 348
[Gear (86)] created with speed: 695
[Gear (87)] created with speed: 618
[Gear (88)] created with speed: 986
[Gear (89)] created with speed: 624
[Gear (90)] created with speed: 578
[Gear (91)] created with speed: 613
[Gear (92)] created with speed: 52
[Gear (93)] created with speed: 9
[Gear (94)] created with speed: 91
[Gear (95)] created with speed: 790
[Gear (96)] created with speed: 481
[Gear (97)] created with speed: 443
[Gear (98)] created with speed: 709
[Gear (99)] created with speed: 772
[Controller] Send commands for syncing to gears!
[Controller] calculated syncSpeed: 512
[Controller] started all gears
[Gear (38)] has syncSpeed
[Controller] Syncspeed received by a gear (38)
[Gear (27)] has syncSpeed
[Controller] Syncspeed received by a gear (27)
[Gear (73)] has syncSpeed
[Controller] Syncspeed received by a gear (73)
[Gear (96)] has syncSpeed
[Controller] Syncspeed received by a gear (96)
[Gear (69)] has syncSpeed
[Controller] Syncspeed received by a gear (69)
[Gear (68)] has syncSpeed
[Controller] Syncspeed received by a gear (68)
[Gear (57)] has syncSpeed
[Controller] Syncspeed received by a gear (57)
[Gear (4)] has syncSpeed
[Controller] Syncspeed received by a gear (4)
[Gear (30)] has syncSpeed
[Controller] Syncspeed received by a gear (30)
[Gear (90)] has syncSpeed
[Controller] Syncspeed received by a gear (90)
[Gear (97)] has syncSpeed
[Controller] Syncspeed received by a gear (97)
[Gear (9)] has syncSpeed
[Controller] Syncspeed received by a gear (9)
[Gear (80)] has syncSpeed
[Controller] Syncspeed received by a gear (80)
[Gear (49)] has syncSpeed
[Controller] Syncspeed received by a gear (49)
[Gear (91)] has syncSpeed
[Controller] Syncspeed received by a gear (91)
[Gear (58)] has syncSpeed
[Controller] Syncspeed received by a gear (58)
[Gear (87)] has syncSpeed
[Controller] Syncspeed received by a gear (87)
[Gear (82)] has syncSpeed
[Gear (89)] has syncSpeed
[Controller] Syncspeed received by a gear (82)
[Controller] Syncspeed received by a gear (89)
[Gear (1)] has syncSpeed
[Controller] Syncspeed received by a gear (1)
[Gear (12)] has syncSpeed
[Controller] Syncspeed received by a gear (12)
[Gear (37)] has syncSpeed
[Controller] Syncspeed received by a gear (37)
[Gear (17)] has syncSpeed
[Controller] Syncspeed received by a gear (17)
[Gear (24)] has syncSpeed
[Controller] Syncspeed received by a gear (24)
[Gear (52)] has syncSpeed
[Controller] Syncspeed received by a gear (52)
[Gear (77)] has syncSpeed
[Controller] Syncspeed received by a gear (77)
[Gear (28)] has syncSpeed
[Controller] Syncspeed received by a gear (28)
[Gear (85)] has syncSpeed
[Controller] Syncspeed received by a gear (85)
[Gear (8)] has syncSpeed
[Controller] Syncspeed received by a gear (8)
[Gear (15)] has syncSpeed
[Gear (67)] has syncSpeed
[Controller] Syncspeed received by a gear (15)
[Controller] Syncspeed received by a gear (67)
[Gear (59)] has syncSpeed
[Controller] Syncspeed received by a gear (59)
[Gear (86)] has syncSpeed
[Controller] Syncspeed received by a gear (86)
[Gear (98)] has syncSpeed
[Controller] Syncspeed received by a gear (98)
[Gear (46)] has syncSpeed
[Controller] Syncspeed received by a gear (46)
[Gear (26)] has syncSpeed
[Controller] Syncspeed received by a gear (26)
[Gear (63)] has syncSpeed
[Gear (22)] has syncSpeed
[Controller] Syncspeed received by a gear (63)
[Gear (5)] has syncSpeed
[Controller] Syncspeed received by a gear (22)
[Controller] Syncspeed received by a gear (5)
[Gear (45)] has syncSpeed
[Controller] Syncspeed received by a gear (45)
[Gear (74)] has syncSpeed
[Gear (50)] has syncSpeed
[Controller] Syncspeed received by a gear (74)
[Gear (81)] has syncSpeed
[Controller] Syncspeed received by a gear (50)
[Controller] Syncspeed received by a gear (81)
[Gear (33)] has syncSpeed
[Controller] Syncspeed received by a gear (33)
[Gear (99)] has syncSpeed
[Controller] Syncspeed received by a gear (99)
[Gear (25)] has syncSpeed
[Controller] Syncspeed received by a gear (25)
[Gear (48)] has syncSpeed
[Controller] Syncspeed received by a gear (48)
[Gear (14)] has syncSpeed
[Controller] Syncspeed received by a gear (14)
[Gear (16)] has syncSpeed
[Gear (79)] has syncSpeed
[Controller] Syncspeed received by a gear (16)
[Controller] Syncspeed received by a gear (79)
[Gear (56)] has syncSpeed
[Controller] Syncspeed received by a gear (56)
[Gear (43)] has syncSpeed
[Controller] Syncspeed received by a gear (43)
[Gear (19)] has syncSpeed
[Controller] Syncspeed received by a gear (19)
[Gear (66)] has syncSpeed
[Gear (10)] has syncSpeed
[Controller] Syncspeed received by a gear (66)
[Controller] Syncspeed received by a gear (10)
[Gear (31)] has syncSpeed
[Controller] Syncspeed received by a gear (31)
[Gear (55)] has syncSpeed
[Gear (95)] has syncSpeed
[Controller] Syncspeed received by a gear (55)
[Controller] Syncspeed received by a gear (95)
[Gear (61)] has syncSpeed
[Controller] Syncspeed received by a gear (61)
[Gear (51)] has syncSpeed
[Controller] Syncspeed received by a gear (51)
[Gear (60)] has syncSpeed
[Controller] Syncspeed received by a gear (60)
[Gear (29)] has syncSpeed
[Controller] Syncspeed received by a gear (29)
[Gear (34)] has syncSpeed
[Controller] Syncspeed received by a gear (34)
[Gear (7)] has syncSpeed
[Controller] Syncspeed received by a gear (7)
[Gear (11)] has syncSpeed
[Controller] Syncspeed received by a gear (11)
[Gear (47)] has syncSpeed
[Controller] Syncspeed received by a gear (47)
[Gear (71)] has syncSpeed
[Controller] Syncspeed received by a gear (71)
[Gear (23)] has syncSpeed
[Controller] Syncspeed received by a gear (23)
[Gear (35)] has syncSpeed
[Gear (2)] has syncSpeed
[Controller] Syncspeed received by a gear (35)
[Controller] Syncspeed received by a gear (2)
[Gear (54)] has syncSpeed
[Controller] Syncspeed received by a gear (54)
[Gear (32)] has syncSpeed
[Controller] Syncspeed received by a gear (32)
[Gear (21)] has syncSpeed
[Controller] Syncspeed received by a gear (21)
[Gear (44)] has syncSpeed
[Controller] Syncspeed received by a gear (44)
[Gear (75)] has syncSpeed
[Controller] Syncspeed received by a gear (75)
[Gear (36)] has syncSpeed
[Controller] Syncspeed received by a gear (36)
[Gear (18)] has syncSpeed
[Gear (76)] has syncSpeed
[Controller] Syncspeed received by a gear (76)
[Controller] Syncspeed received by a gear (18)
[Gear (41)] has syncSpeed
[Controller] Syncspeed received by a gear (41)
[Gear (78)] has syncSpeed
[Gear (40)] has syncSpeed
[Controller] Syncspeed received by a gear (78)
[Controller] Syncspeed received by a gear (40)
[Gear (42)] has syncSpeed
[Gear (6)] has syncSpeed
[Controller] Syncspeed received by a gear (42)
[Controller] Syncspeed received by a gear (6)
[Gear (94)] has syncSpeed
[Controller] Syncspeed received by a gear (94)
[Gear (0)] has syncSpeed
[Gear (84)] has syncSpeed
[Controller] Syncspeed received by a gear (0)
[Gear (83)] has syncSpeed
[Controller] Syncspeed received by a gear (83)
[Gear (72)] has syncSpeed
[Controller] Syncspeed received by a gear (72)
[Gear (20)] has syncSpeed
[Controller] Syncspeed received by a gear (20)
[Gear (92)] has syncSpeed
[Controller] Syncspeed received by a gear (84)
[Controller] Syncspeed received by a gear (92)
[Gear (13)] has syncSpeed
[Gear (3)] has syncSpeed
[Controller] Syncspeed received by a gear (13)
[Controller] Syncspeed received by a gear (3)
[Gear (70)] has syncSpeed
[Controller] Syncspeed received by a gear (70)
[Gear (88)] has syncSpeed
[Controller] Syncspeed received by a gear (88)
[Gear (53)] has syncSpeed
[Gear (64)] has syncSpeed
[Gear (62)] has syncSpeed
[Gear (93)] has syncSpeed
[Gear (39)] has syncSpeed
[Gear (65)] has syncSpeed
[Controller] Syncspeed received by a gear (53)
[Controller] Syncspeed received by a gear (64)
[Controller] Syncspeed received by a gear (62)
[Controller] Syncspeed received by a gear (93)
[Controller] Syncspeed received by a gear (39)
[Controller] Syncspeed received by a gear (65)
[Controller] all gears are back in town!

You can see an alternating output of the different actors (gears and controller). But if I change to scala 2.8 I get an output like this:

[App] start with creating gears
[Gear (0)] created with speed: 995
[Gear (1)] created with speed: 40
[Gear (2)] created with speed: 483
[Gear (3)] created with speed: 920
[Gear (4)] created with speed: 339
[Gear (5)] created with speed: 422
[Gear (6)] created with speed: 258
[Gear (7)] created with speed: 603
[Gear (8)] created with speed: 40
[Gear (9)] created with speed: 399
[Gear (10)] created with speed: 347
[Gear (11)] created with speed: 339
[Gear (12)] created with speed: 559
[Gear (13)] created with speed: 697
[Gear (14)] created with speed: 18
[Gear (15)] created with speed: 568
[Gear (16)] created with speed: 271
[Gear (17)] created with speed: 239
[Gear (18)] created with speed: 889
[Gear (19)] created with speed: 933
[Gear (20)] created with speed: 793
[Gear (21)] created with speed: 494
[Gear (22)] created with speed: 683
[Gear (23)] created with speed: 41
[Gear (24)] created with speed: 738
[Gear (25)] created with speed: 291
[Gear (26)] created with speed: 833
[Gear (27)] created with speed: 610
[Gear (28)] created with speed: 514
[Gear (29)] created with speed: 793
[Gear (30)] created with speed: 917
[Gear (31)] created with speed: 838
[Gear (32)] created with speed: 801
[Gear (33)] created with speed: 153
[Gear (34)] created with speed: 19
[Gear (35)] created with speed: 549
[Gear (36)] created with speed: 524
[Gear (37)] created with speed: 912
[Gear (38)] created with speed: 517
[Gear (39)] created with speed: 586
[Gear (40)] created with speed: 662
[Gear (41)] created with speed: 143
[Gear (42)] created with speed: 147
[Gear (43)] created with speed: 445
[Gear (44)] created with speed: 1
[Gear (45)] created with speed: 366
[Gear (46)] created with speed: 397
[Gear (47)] created with speed: 523
[Gear (48)] created with speed: 920
[Gear (49)] created with speed: 91
[Gear (50)] created with speed: 316
[Gear (51)] created with speed: 573
[Gear (52)] created with speed: 956
[Gear (53)] created with speed: 615
[Gear (54)] created with speed: 636
[Gear (55)] created with speed: 385
[Gear (56)] created with speed: 422
[Gear (57)] created with speed: 574
[Gear (58)] created with speed: 882
[Gear (59)] created with speed: 274
[Gear (60)] created with speed: 371
[Gear (61)] created with speed: 971
[Gear (62)] created with speed: 999
[Gear (63)] created with speed: 990
[Gear (64)] created with speed: 367
[Gear (65)] created with speed: 646
[Gear (66)] created with speed: 957
[Gear (67)] created with speed: 187
[Gear (68)] created with speed: 432
[Gear (69)] created with speed: 472
[Gear (70)] created with speed: 899
[Gear (71)] created with speed: 325
[Gear (72)] created with speed: 615
[Gear (73)] created with speed: 315
[Gear (74)] created with speed: 425
[Gear (75)] created with speed: 441
[Gear (76)] created with speed: 679
[Gear (77)] created with speed: 125
[Gear (78)] created with speed: 706
[Gear (79)] created with speed: 455
[Gear (80)] created with speed: 15
[Gear (81)] created with speed: 155
[Gear (82)] created with speed: 669
[Gear (83)] created with speed: 590
[Gear (84)] created with speed: 457
[Gear (85)] created with speed: 574
[Gear (86)] created with speed: 1
[Gear (87)] created with speed: 472
[Gear (88)] created with speed: 763
[Gear (89)] created with speed: 329
[Gear (90)] created with speed: 385
[Gear (91)] created with speed: 176
[Gear (92)] created with speed: 674
[Gear (93)] created with speed: 528
[Gear (94)] created with speed: 702
[Gear (95)] created with speed: 328
[Gear (96)] created with speed: 82
[Gear (97)] created with speed: 833
[Gear (98)] created with speed: 101
[Gear (99)] created with speed: 950
[Controller] Send commands for syncing to gears!
[Controller] calculated syncSpeed: 504
[Controller] started all gears
[Gear (2)] has syncSpeed
[Gear (0)] has syncSpeed
[Gear (5)] has syncSpeed
[Gear (1)] has syncSpeed
[Gear (4)] has syncSpeed
[Gear (8)] has syncSpeed
[Gear (9)] has syncSpeed
[Gear (7)] has syncSpeed
[Gear (10)] has syncSpeed
[Gear (11)] has syncSpeed
[Gear (12)] has syncSpeed
[Gear (13)] has syncSpeed
[Gear (15)] has syncSpeed
[Gear (14)] has syncSpeed
[Gear (3)] has syncSpeed
[Gear (16)] has syncSpeed
[Gear (17)] has syncSpeed
[Gear (18)] has syncSpeed
[Gear (21)] has syncSpeed
[Gear (6)] has syncSpeed
[Gear (22)] has syncSpeed
[Gear (24)] has syncSpeed
[Gear (23)] has syncSpeed
[Gear (25)] has syncSpeed
[Gear (20)] has syncSpeed
[Gear (28)] has syncSpeed
[Gear (29)] has syncSpeed
[Gear (26)] has syncSpeed
[Gear (19)] has syncSpeed
[Gear (32)] has syncSpeed
[Gear (31)] has syncSpeed
[Gear (33)] has syncSpeed
[Gear (35)] has syncSpeed
[Gear (36)] has syncSpeed
[Gear (30)] has syncSpeed
[Gear (38)] has syncSpeed
[Gear (39)] has syncSpeed
[Gear (34)] has syncSpeed
[Gear (40)] has syncSpeed
[Gear (41)] has syncSpeed
[Gear (42)] has syncSpeed
[Gear (27)] has syncSpeed
[Gear (45)] has syncSpeed
[Gear (46)] has syncSpeed
[Gear (47)] has syncSpeed
[Gear (44)] has syncSpeed
[Gear (43)] has syncSpeed
[Gear (48)] has syncSpeed
[Gear (50)] has syncSpeed
[Gear (51)] has syncSpeed
[Gear (49)] has syncSpeed
[Gear (53)] has syncSpeed
[Gear (37)] has syncSpeed
[Gear (54)] has syncSpeed
[Gear (56)] has syncSpeed
[Gear (57)] has syncSpeed
[Gear (59)] has syncSpeed
[Gear (58)] has syncSpeed
[Gear (60)] has syncSpeed
[Gear (55)] has syncSpeed
[Gear (52)] has syncSpeed
[Gear (62)] has syncSpeed
[Gear (63)] has syncSpeed
[Gear (64)] has syncSpeed
[Gear (65)] has syncSpeed
[Gear (67)] has syncSpeed
[Gear (68)] has syncSpeed
[Gear (69)] has syncSpeed
[Gear (61)] has syncSpeed
[Gear (70)] has syncSpeed
[Gear (72)] has syncSpeed
[Gear (66)] has syncSpeed
[Gear (74)] has syncSpeed
[Gear (73)] has syncSpeed
[Gear (75)] has syncSpeed
[Gear (76)] has syncSpeed
[Gear (79)] has syncSpeed
[Gear (71)] has syncSpeed
[Gear (78)] has syncSpeed
[Gear (77)] has syncSpeed
[Gear (82)] has syncSpeed
[Gear (80)] has syncSpeed
[Gear (84)] has syncSpeed
[Gear (83)] has syncSpeed
[Gear (85)] has syncSpeed
[Gear (87)] has syncSpeed
[Gear (88)] has syncSpeed
[Gear (81)] has syncSpeed
[Gear (86)] has syncSpeed
[Gear (89)] has syncSpeed
[Gear (90)] has syncSpeed
[Gear (93)] has syncSpeed
[Gear (94)] has syncSpeed
[Gear (91)] has syncSpeed
[Gear (95)] has syncSpeed
[Gear (96)] has syncSpeed
[Gear (92)] has syncSpeed
[Gear (97)] has syncSpeed
[Controller] Syncspeed received by a gear (2)
[Controller] Syncspeed received by a gear (0)
[Controller] Syncspeed received by a gear (5)
[Controller] Syncspeed received by a gear (1)
[Controller] Syncspeed received by a gear (4)
[Controller] Syncspeed received by a gear (8)
[Controller] Syncspeed received by a gear (9)
[Controller] Syncspeed received by a gear (7)
[Controller] Syncspeed received by a gear (10)
[Controller] Syncspeed received by a gear (11)
[Controller] Syncspeed received by a gear (12)
[Controller] Syncspeed received by a gear (13)
[Controller] Syncspeed received by a gear (15)
[Controller] Syncspeed received by a gear (14)
[Controller] Syncspeed received by a gear (3)
[Controller] Syncspeed received by a gear (16)
[Controller] Syncspeed received by a gear (17)
[Controller] Syncspeed received by a gear (18)
[Controller] Syncspeed received by a gear (21)
[Controller] Syncspeed received by a gear (6)
[Controller] Syncspeed received by a gear (22)
[Controller] Syncspeed received by a gear (24)
[Controller] Syncspeed received by a gear (23)
[Controller] Syncspeed received by a gear (25)
[Controller] Syncspeed received by a gear (20)
[Controller] Syncspeed received by a gear (28)
[Controller] Syncspeed received by a gear (29)
[Controller] Syncspeed received by a gear (26)
[Controller] Syncspeed received by a gear (19)
[Controller] Syncspeed received by a gear (32)
[Controller] Syncspeed received by a gear (31)
[Controller] Syncspeed received by a gear (33)
[Controller] Syncspeed received by a gear (35)
[Controller] Syncspeed received by a gear (36)
[Controller] Syncspeed received by a gear (30)
[Controller] Syncspeed received by a gear (38)
[Controller] Syncspeed received by a gear (39)
[Controller] Syncspeed received by a gear (34)
[Controller] Syncspeed received by a gear (40)
[Controller] Syncspeed received by a gear (41)
[Controller] Syncspeed received by a gear (42)
[Controller] Syncspeed received by a gear (27)
[Gear (98)] has syncSpeed
[Gear (99)] has syncSpeed
[Controller] Syncspeed received by a gear (45)
[Controller] Syncspeed received by a gear (46)
[Controller] Syncspeed received by a gear (47)
[Controller] Syncspeed received by a gear (44)
[Controller] Syncspeed received by a gear (43)
[Controller] Syncspeed received by a gear (48)
[Controller] Syncspeed received by a gear (50)
[Controller] Syncspeed received by a gear (51)
[Controller] Syncspeed received by a gear (49)
[Controller] Syncspeed received by a gear (53)
[Controller] Syncspeed received by a gear (37)
[Controller] Syncspeed received by a gear (54)
[Controller] Syncspeed received by a gear (56)
[Controller] Syncspeed received by a gear (57)
[Controller] Syncspeed received by a gear (59)
[Controller] Syncspeed received by a gear (58)
[Controller] Syncspeed received by a gear (60)
[Controller] Syncspeed received by a gear (55)
[Controller] Syncspeed received by a gear (52)
[Controller] Syncspeed received by a gear (62)
[Controller] Syncspeed received by a gear (63)
[Controller] Syncspeed received by a gear (64)
[Controller] Syncspeed received by a gear (65)
[Controller] Syncspeed received by a gear (67)
[Controller] Syncspeed received by a gear (68)
[Controller] Syncspeed received by a gear (69)
[Controller] Syncspeed received by a gear (61)
[Controller] Syncspeed received by a gear (70)
[Controller] Syncspeed received by a gear (72)
[Controller] Syncspeed received by a gear (66)
[Controller] Syncspeed received by a gear (74)
[Controller] Syncspeed received by a gear (73)
[Controller] Syncspeed received by a gear (75)
[Controller] Syncspeed received by a gear (76)
[Controller] Syncspeed received by a gear (79)
[Controller] Syncspeed received by a gear (71)
[Controller] Syncspeed received by a gear (78)
[Controller] Syncspeed received by a gear (77)
[Controller] Syncspeed received by a gear (82)
[Controller] Syncspeed received by a gear (80)
[Controller] Syncspeed received by a gear (84)
[Controller] Syncspeed received by a gear (83)
[Controller] Syncspeed received by a gear (85)
[Controller] Syncspeed received by a gear (87)
[Controller] Syncspeed received by a gear (88)
[Controller] Syncspeed received by a gear (81)
[Controller] Syncspeed received by a gear (86)
[Controller] Syncspeed received by a gear (89)
[Controller] Syncspeed received by a gear (90)
[Controller] Syncspeed received by a gear (93)
[Controller] Syncspeed received by a gear (94)
[Controller] Syncspeed received by a gear (91)
[Controller] Syncspeed received by a gear (95)
[Controller] Syncspeed received by a gear (96)
[Controller] Syncspeed received by a gear (92)
[Controller] Syncspeed received by a gear (97)
[Controller] Syncspeed received by a gear (98)
[Controller] Syncspeed received by a gear (99)
[Controller] all gears are back in town!

You can see that there is much less alternating actor-interactions. I suspect that the controller is blocked while the gears are working. Could someone explain this behavior? Thanks Philipp

+3  A: 

There are some problems with the code. Foremost among them, don't do this:

object ActorApplication extends Application 

When you use extends Application, the way your code is run imposes a number of restrictions, such as not being optimizable by the JIT and, of particular relevance to your case, threads won't work correctly. I find it harder to explain what happened on 2.7.7 than what happened on 2.8. Instead, just write a normal object, and define the main method.

Other stuff:

gearController ! StartSync()

Don't do this. Always send a message to an actor from inside an actor, and this line is inside ActorApplication constructor, which is not an actor. In situations like this, you can do this:

Actor.actor { gearController ! StartSync() }

Speaking of StartSync, don't do this:

case class StartSync

A class without parameters is meaningless (and deprecated). Instead, do this:

case object StartSync

and drop the parenthesis after StartSync in the places where you use it.

Also, there's nothing wrong with scala.util.Random, though it was easier to misuse it on Scala 2.7 than it is on Scala 2.8, by creating a new Random generator every time you needed a random number (which is what you do in the code). Instead, on Scala 2.8, just this ought to work:

  private var mySpeed = scala.util.Random.nextInt(1000)

Finally, there's nothing wrong with either execution, it's just the scheduler that's different between them. In fact, given that you created a hundred actors, the GearController was receiving a disproportional amount of execution time in the Scala 2.7 version, as Scala doesn't create a thread for every actor.

Now, if you want GearController not to share a thread with the others, you should use while(true)/receive instead of loop/react, like this:

    while(true) {
      receive {
        case StartSync => {

Which will produce a result quite similar to what 2.7 produced.

Daniel
Thank you Daniel for your helpful answer. I did the changes and my code runs well in Scala 2.8. I'll do a simple Scala-swing-GUI for this simulation and publish it on github.
pmeiclx