Scala:为什么Actors轻巧? [英] Scala: Why are Actors lightweight?

查看:78
本文介绍了Scala:为什么Actors轻巧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么让演员如此轻巧?

What makes actors so lightweight?

我什至不知道他们的工作方式。他们不是分开线程吗?

I'm not even sure how they work. Aren't they separate threads?

推荐答案

当他们说轻量级时,意味着每个参与者都没有映射到单个线程。 / p>

When they say lightweight they mean that each actor is not mapped to a single thread.


JVM提供带有
锁的共享内存线程,作为
并发抽象的主要形式。但是共享的
内存线程是重量级的
,并且由于上下文切换开销而导致严重的性能损失
。对于
,基于
与JVM线程一对一映射的actor实现,
每个Scala actor
的进程有效负载不会像我们可以负担的那样轻便b产生一百万个actor
实例进行特定计算。因此,
Scala actor被设计为
个轻量级事件对象,它们在
个底层工作线程池上安排并执行
个,当所有$ b个时,
会自动调整大小$ b线程在长时间运行的
操作中阻塞。实际上,Scala实现了
参与者的统一模型-基于线程
和基于事件。 Scala演员
提供两种形式的悬浮
机制-全栈框架
悬浮(实现为接收)和
a悬浮基于连续的
关闭(实现为react) )。在
基于事件的参与者的情况下,对
反应的等待由连续的
闭包表示,即,闭包捕获了参与者剩余的
的计算量。
当暂停的参与者收到与参与者中指定的
模式之一匹配的
消息时,通过将
的任务调度到其中一个执行该
延续工作线程从基础线程池中获得

论文使线程和$ b $统一的参与者b Halls and Odersky
的 b Events讨论了
实现的详细信息。

JVM offers shared memory threads with locks as the primary form of concurrency abstractions. But shared memory threads are quite heavyweight and incur severe performance penalties from context switching overheads. For an actor implementation based on a one-to-one mapping with JVM threads, the process payload per Scala actor will not be as lightweight that we can spawn a million instances of an actor for a specific computation. Hence Scala actors have been designed as lightweight event objects, which get scheduled and executed on an underlying worker thread pool that gets automatically resized when all threads block on long running operations. In fact, Scala implements a unified model of actors - thread based and event based. Scala actors offer two form of suspension mechanisms - a full stack frame suspension(implemented as receive) and a suspension based on a continuation closure (implemented as react). In case of event based actors, a wait on react is represented by a continuation closure, i.e. a closure that captures the rest of the actor's computation. When the suspended actor receives a message that matches one of the patterns specified in the actor, the continuation is executed by scheduling the task to one of the worker threads from the underlying thread pool. The paper "Actors that Unify Threads and Events" by Haller and Odersky discusses the details of the implementation.

来源

这篇关于Scala:为什么Actors轻巧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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