什么时候为 Scala 演员的反应创建线程?

编程入门 行业动态 更新时间:2024-10-12 16:29:09
本文介绍了什么时候为 Scala 演员的反应创建线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在阅读了在 Scala 的 actor 中使用 react 之后,我认为 react 会共享同一个线程,因为没有多个 react> 待定.好像不是这样.

After reading about using react in actors in Scala, I thought react's would share the same thread given there weren't multiple react's pending. It doesn't seem to be the case.

import scala.actors.Actor import scala.actors.Actor._ class SleepyReactor extends Actor { def act() { loop { react { case x => { println("reacting to %s on thread %s".format(x, Thread.currentThread.getName)) Thread.sleep(1000) println("done with " + x) } } } } } val sleepyOne = new SleepyReactor sleepyOne.start sleepyOne ! "first" // runs on thread-5 // wait until completion sleepyOne ! "second" // runs on thread-3

有人可以解释为什么这些 react 运行在不同的线程上,以及何时为具有 react 的 actor 创建新线程?

Can someone explain why these react's are running on different threads and when a new thread is created for an actor with react?

我在某处读到 react 是基于事件的,我认为这意味着反应演员"共享一个线程,如果一个反应演员"正在反应",其他反应演员"将排队,直到首先完成了.我现在觉得我错了.这是如何工作的,它与接收有何不同?

I read somewhere react is event based, and I took that to mean that "react actors" shared a thread and if one was "reacting" the other "react actors" would be queued until the first was done. I now think I am wrong. How does this work, and how is it different than receive?

推荐答案

对于纯基于事件的actor来说,它的反应代码与消息发送代码运行在同一线程上.

It is true that for a pure event-based actor, its reacting code runs on the same thread as message sending code.

但是在 Scala 中,由于当 Actor 在其 React 代码中调用阻塞操作时阻塞线程并统一基于事件和基于线程的 Actor(能够组合它们)是不可取的,因此两种类型的 Actor 都使用相同的线程池,但基于线程的参与者获得自己的线程,而基于事件的参与者基于任务队列共享线程.有关详细信息,请参阅 Philipp Haller 和 统一线程和事件的演员和马丁·奥德斯基

But in Scala, since it's not desirable to block a thread when an actor calls a blocking operation inside its react code and to unify event-based and thread-based actors(being able to compose them), both type of actors uses the same thread pool but the thread-based actors get their own threads whereas event-based actors shares threads based on a task queue. For details, please see Actors that Unify Threads and Events by Philipp Haller and Martin Odersky

更多推荐

什么时候为 Scala 演员的反应创建线程?

本文发布于:2023-11-25 15:06:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630228.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:什么时候   线程   演员   Scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!