- 当使用receive时,程序为每个actor都单独创建一个线程,如没有接收到消息,该线程也会一直保持等待,不可以其他任务复用。
- 当使用react时,actor不会单独占用线程,只有当接收到消息程序才会为该actor分配线程,非常节省资源。
但毫无疑问,使用react时每接受到一条消息都需要重新分配线程和进行初始化工作,所以会有一定的延迟,这在实际应用中也需要进行权衡。
以下代码展示了上述的区别:WRActor和LRActor分别代表了两种接收消息的模式,WRActor接收每条消息都新建了一个线程,而LRActor接收所有消息都复用了相同的线程。
import scala.actors.Actor
object Sub6 extends App {
1 to 30 foreach{ i =>
new WRActor().start() ! "Hello"
}
1 to 30 foreach{ i =>
new LRActor().start() ! "Hello"
}
}
class WRActor extends Actor{
override def act(): Unit = {
while(true){
receive{
case "Hello" => println("WR: "+Thread.currentThread())
}
}
}
}
class LRActor extends Actor{
override def act(): Unit = {
loop{
react{
case "Hello" => println("LR: "+Thread.currentThread())
}
}
}
}
WR: Thread[ForkJoinPool-1-worker-11,5,main]
WR: Thread[ForkJoinPool-1-worker-7,5,main]
WR: Thread[ForkJoinPool-1-worker-5,5,main]
WR: Thread[ForkJoinPool-1-worker-9,5,main]
WR: Thread[ForkJoinPool-1-worker-13,5,main]
WR: Thread[ForkJoinPool-1-worker-3,5,main]
WR: Thread[ForkJoinPool-1-worker-15,5,main]
WR: Thread[ForkJoinPool-1-worker-1,5,main]
WR: Thread[ForkJoinPool-1-worker-23,5,main]
WR: Thread[ForkJoinPool-1-worker-27,5,main]
WR: Thread[ForkJoinPool-1-worker-31,5,main]
WR: Thread[ForkJoinPool-1-worker-17,5,main]
WR: Thread[ForkJoinPool-1-worker-21,5,main]
WR: Thread[ForkJoinPool-1-worker-19,5,main]
WR: Thread[ForkJoinPool-1-worker-25,5,main]
WR: Thread[ForkJoinPool-1-worker-29,5,main]
WR: Thread[ForkJoinPool-1-worker-47,5,main]
WR: Thread[ForkJoinPool-1-worker-45,5,main]
WR: Thread[ForkJoinPool-1-worker-59,5,main]
WR: Thread[ForkJoinPool-1-worker-39,5,main]
WR: Thread[ForkJoinPool-1-worker-53,5,main]
WR: Thread[ForkJoinPool-1-worker-37,5,main]
WR: Thread[ForkJoinPool-1-worker-51,5,main]
WR: Thread[ForkJoinPool-1-worker-35,5,main]
WR: Thread[ForkJoinPool-1-worker-49,5,main]
WR: Thread[ForkJoinPool-1-worker-63,5,main]
WR: Thread[ForkJoinPool-1-worker-43,5,main]
WR: Thread[ForkJoinPool-1-worker-57,5,main]
WR: Thread[ForkJoinPool-1-worker-41,5,main]
WR: Thread[ForkJoinPool-1-worker-55,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]
LR: Thread[ForkJoinPool-1-worker-33,5,main]