我是一名18岁的实习生,我发现了非常喜欢的scala :-)。 为了熟悉scala演员,我用一些齿轮和一个控制器写了一个小模拟。 ActorApplication以随机速度创建N个齿轮。控制器计算同步速度并启动齿轮转子。齿轮逐步同步到该给定速度(1+或1-)。当所有档位达到同步速度时,模拟结束。 我在scala 2.7.7中开发了模拟 - 它按照我的预期工作(参见下面的输出)。但是,当我发现当前的2.8.0-SNAPSHOT时,我发现了奇怪的行为。 这是代码:
/**
* 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)
}
}
上面的代码在scala 2.7.7中生成如下输出:
[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!
您可以看到不同演员(齿轮和控制器)的交替输出。但是,如果我改为scala 2.8,我会得到这样的输出:
[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!
你可以看到交替的演员互动要少得多。我怀疑在齿轮工作时控制器被阻挡了。有人可以解释这种行为吗?谢谢 菲利普
答案 0 :(得分:3)
代码存在一些问题。其中最重要的是,不要这样做:
object ActorApplication extends Application
当您使用extends Application
时,代码的运行方式会产生许多限制,例如JIT无法优化,并且与您的情况特别相关,线程将无法正常工作。我发现在2.7.7上发生的事情要比在2.8上发生的情况更难解释。相反,只需编写一个普通对象,并定义main
方法。
其他东西:
gearController ! StartSync()
不要这样做。始终从actor内部向actor发送消息,并且此行位于ActorApplication
构造函数内,该构造函数不是actor。在这种情况下,你可以这样做:
Actor.actor { gearController ! StartSync() }
说到StartSync
,请不要这样做:
case class StartSync
没有参数的类是没有意义的(并且已弃用)。相反,这样做:
case object StartSync
并将StartSync
后面的括号放在您使用它的地方。
此外,scala.util.Random
没有任何问题,但是每次需要随机数时,通过创建新的Random
生成器,在Scala 2.7上滥用它比在Scala 2.8上更容易。这是你在代码中所做的)。相反,在Scala 2.8上,这应该是有效的:
private var mySpeed = scala.util.Random.nextInt(1000)
最后,执行中没有任何问题,只是它们之间的调度程序不同。事实上,鉴于你创建了一百个actor,GearController
在Scala 2.7版本中获得了不成比例的执行时间,因为Scala没有为每个actor创建一个线程。
现在,如果您希望GearController
不与其他人分享主题,则应使用while(true)
/ receive
代替loop
/ react
,像这样:
while(true) {
receive {
case StartSync => {
这将产生与2.7产生的结果非常相似的结果。