今年秋天我将参加分布式系统课程,由于 Scala 演员在这个领域非常有用,所以我刚刚开始学习 Scala。从到目前为止我从文本“Scala 中的演员”中读到的有关演员的内容中,我有一个问题似乎在文本中没有得到非常清楚的解决。
我目前正在一台具有多核处理器的机器上对我的程序进行建模,并在 Master-Workers 模型中使用基于事件的参与者。现在,以下两种设计选择中的哪一种会导致所有处理器内核得到最佳使用?
1) 将演员的数量限制在一个固定的小数目,不管主演员有多少工作,分发给工人演员(顺便说一下,有时可能相当大)
2) 生成与 Master Actor 需要完成的作业数量一样多的 Actor
哪些因素会影响我对上述两种选择的决定?
更新:我打印出我正在运行的 100 个工人演员的线程 ID,这是输出 -
No. of Processors-2
MainActor is on thread:Thread[ForkJoinPool-1-worker-0,5,main]
AccumulatorActor is on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-1 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-4 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-5 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-6 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-7 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-8 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-9 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-10 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-3 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-12 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-13 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-14 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-15 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-2 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-16 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-11 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-17 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-18 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-19 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-20 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-21 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-22 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-23 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-24 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-25 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-26 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-28 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-29 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-27 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-30 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-31 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-33 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-32 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-34 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-35 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-36 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-38 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-39 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-40 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-41 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-42 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-43 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-44 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-45 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-46 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-47 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-48 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-51 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-50 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-54 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-49 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-56 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-57 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-58 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-55 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-59 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-52 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-61 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-60 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-63 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-53 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-65 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-66 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-68 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-64 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-62 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-70 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-69 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-67 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-72 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-73 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-71 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-75 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-78 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-79 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-80 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-81 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-82 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-83 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-74 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-84 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-85 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-77 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-76 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-86 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-87 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-88 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-89 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-91 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-92 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-94 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-95 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-96 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-90 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-98 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-99 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-100 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-93 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-37 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-97 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
如上所示,在 ForkJoinPool 中运行了四个工作线程,而我的机器上有 2 个 cpu 内核。我不确定为什么会这样,因为快速查看 ResizableThreadPoolScheduler 的来源让我相信最初池中的工作线程数等于 cpu 核心数,并且这些是动态增加的,以防任何演员在池中的一个工作线程上被阻塞。我使用的所有演员都是基于事件的(反应)并且不执行任何阻塞操作。一个理想的场景是在 ForkJoinPool 中只有 2 个线程,并让它们在执行每个参与者工作的两个核心上并行运行。我对此的理解是正确的还是我在这里遗漏了什么?