Scala Actors:如果react永不返回,为什么它必须处于循环状态{},为什么while(true)不起作用? [英] Scala Actors: if react never returns, why does it need to be in a loop{}, and why doesn't while(true) work?

查看:103
本文介绍了Scala Actors:如果react永不返回,为什么它必须处于循环状态{},为什么while(true)不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scala Actor入门。 Scala网站说:

Just getting started on Scala Actors. The Scala website says:


使用 react 来等待
新消息(<$ c的基于事件的挂件)可以避免线程阻塞操作$ c>接收)。但是,需要支付
(通常很小)的价格:反应永不返回。

Thread-blocking operations can be avoided by using react to wait for new messages (the event-based pendant of receive). However, there is a (usually small) price to pay: react never returns.

。 ..

请注意,在 while 内使用反应循环不起作用!但是,由于循环是常见的
,因此它在 loop 函数的形式
中提供了特殊的库支持。可以这样使用:

Note that using react inside a while loop does not work! However, since loops are common there is special library support for it in form of a loop function. It can be used like this:



loop {
  react {
    case A => ...
    case B => ...
  }
}

我现在很困惑-似乎是矛盾的:

I'm now confused - there seems to be a contradiction:

a)如果 react 永不返回,那么将其置于循环中的意义何在? ?

a) If react never returns, then what's the point of putting it in a loop?

b)由于循环重复执行一个块,它与有何不同? while(true)-为什么 while 不起作用,为什么它不起作用?

b) Since loop repeatedly executes a block, how is it any different to while(true) - why doesn't while work, and in what way does it "not work"?

推荐答案

两个函数,循环反应不纯 loop 通过名称参数进行调用,而 react 进行PartialFunction调用,两者均在原始actor上设置变量。这是因为actor始终没有附加线程。仅当其消息框中有一条消息时,它才会激活。这就是为什么 while(true)会导致100%的CPU使用率并且参与者没有响应的原因。

Both functions, loop and react are not pure. loop takes a call by name parameter and react a PartialFunction, both set variables on the raw actor. This is because an actor does not have a thread attached all the time. It will become active only when there is a message in it's messagebox. This is why a while(true) will lead to 100% cpu usage and the actor not responding.

这篇关于Scala Actors:如果react永不返回,为什么它必须处于循环状态{},为什么while(true)不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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