在尝试比较Java和Scala中实现的同一个简单问题的"执行特性"时,我发现Java版本中的线程类在名为 Thread-x的线程上运行,而Scala actor在名称类似的线程上运行ForkJoinPool-x-worker-y.非线程Java类和非actor actor Scala类始终在主线程上运行.
下面的日志片段将说明:
Java version: Scoord thread class running in Thread[Thread-0,5,main]
Semaphore class running in Thread[main,5,main]
Scala version: Scoord actor running in Thread[ForkJoinPool-1-worker-13,5,main]
Semaphore class running in Thread[main,5,main]
Run Code Online (Sandbox Code Playgroud)
除了作为不同类的实例 - Thread类和ForkJoinWorkerThread类之外,这些线程之间有什么区别?工作线程如何映射到O/S内核线程以执行?
关于用于JVM线程的命名约定的任何解释也将非常受欢迎.
两个实现都在相同版本的JVM上运行 - HotSpot(TM)64位服务器VM(内置23.21-b01,混合模式).硬件是64位,4核Acer笔记本电脑,内存为8G,运行Windows 8.
我在Horstman书(第291-292页)中的示例代码中定义和使用案例类 作为简单Scala actor系统中的消息.
问题是在接收模式匹配中没有识别类,并且控制正在落入case _ =>语句.
代码如下所示.一切都适用于非案例类消息.
SENDER:在演员Rcoord中,行为方法是:
def act() {
alive(9000)
register('rcoord, self)
proc_reg.start // start the process register actor
loop {
try {
receive {
case 'process =>
process_counter += 1
process_number = process_counter
spawn_process(process_number, sprocs)
case class CreateTS(xxx: Int)
proc_reg ! CreateTS(process_number)
case 'stats =>
Console.println("received msg from client to view statistics")
//sender ! 'ok
case 'stop =>
Console.println("received msg that client is terminating")
//sender ! 'bye
} // end receive …Run Code Online (Sandbox Code Playgroud)