Scala中的Actor不能处理消息吗? (例如O'Reilly的Programming Scala中的示例) [英] Can Actors in Scala fail to process messages? (example in O'Reilly's Programming Scala)

查看:47
本文介绍了Scala中的Actor不能处理消息吗? (例如O'Reilly的Programming Scala中的示例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Scala完全陌生,并且一直在通过对Scala编程(O'Reilly)在线工作;这样做时,我对shapes-actor-script.scala 示例的结果感到惊讶.com / ch01.html#ATasteOfConcurrency rel = nofollow>第1章,并发口味 。

I'm completely new to Scala, and I've been working my way through Programming Scala (O'Reilly) online; while doing so, I was surprised by the result of the shapes-actor-script.scala example in Chapter 1, "A Taste Of Concurrency".

具体来说,运行的输出 scala -cp。 Shapes-actor-script.scala 应该为:

Specifically, the output of running scala -cp . shapes-actor-script.scala should be:

Circle.draw: Circle(Point(0.0,0.0),1.0)
Rectangle.draw: Rectangle(Point(0.0,0.0),2.0,5.0)
Triangle.draw: Triangle(Point(0.0,0.0),Point(1.0,0.0),Point(0.0,1.0))
Error: Unknown message! 3.14159
exiting...

但是大约10%的时间我没有输出甚至很少,我只会得到第一行作为输出。我对Scala的了解还不够,还不知道由于Actors的工作方式是否正常,或者我的Scala安装(Arch Linux上的Scala 2.8.1)是否有问题。

Yet about 10% of the time, I get no output at all, and even more rarely, I'll get only the first line as output. I don't know enough about Scala yet to know if this is normal due to the way Actors work, or if something might be wrong with my Scala installation (Scala 2.8.1 on Arch Linux).

Actor能否无法像这样处理消息(也许是由于示例的编写方式)?还是在发生其他事情,我可能会在这里丢失?

Can Actors fail to process messages like this (perhaps because of the way the example is written)? Or is there something else going on that I might be missing here?

推荐答案

我相信Scala REPL正在使用System.exit( ...)完成脚本运行后。这将停止进程,而无需等待任何缠绵的线程。

I believe the Scala REPL is using System.exit(...) when it is finished running the script. That will stop the process without waiting for any lingering threads.

这意味着所有消息都将发送给演员,但演员可能无法及时处理它们。

That means that all messages will be sent to the actor, but the actor might not be able to handle them in time.

为演示您可以尝试在shapes-actor.scala中的每种情况下添加Thread.sleep(1000):

To demonstrate you might try to add Thread.sleep(1000) to each of the cases in the shapes-actor.scala:

case s: Shape => Thread.sleep(1000);s.draw()
case "exit"   => Thread.sleep(1000);println("exiting..."); exit
case x: Any   => Thread.sleep(1000);println("Error: Unknown message! " + x)

可能会使脚本每次都失败(它在我的计算机上确实失败)。如果您随后添加Thread.sleep(5000)(给予2秒钟的松弛时间),则每次都应成功。

This will probably make the script fail every time (it does on my machine). If you then add Thread.sleep(5000) (giving 2 secs of slack) it should succeed every time.

解决方案是使用不以结尾结尾的程序。 System.exit(...)。

The solution is to use a program that does not end in System.exit(...).

更新(第二个想法):

您还可以设置参与者在退出时通知:

You can also set up the actor to notify on exit:

case "exit"   => Thread.sleep(1000);println("exiting..."); this.synchronized { this.notify }; exit

...,然后脚本可以等待通知:

... and then the script can wait for the notification:

ShapeDrawingActor.synchronized { ShapeDrawingActor.wait(10000) }

这篇关于Scala中的Actor不能处理消息吗? (例如O'Reilly的Programming Scala中的示例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆