Scala: Why are Actors lightweight?

前端 未结 2 1064
伪装坚强ぢ
伪装坚强ぢ 2021-02-19 18:04

What makes actors so lightweight?

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

相关标签:
2条回答
  • 2021-02-19 18:43

    Important Reference Actors that Unify Threads and Events

    I don't think we should strengthen that actor is that lightweight.

    firstly thread based actors are actor per thread so not lightweight at all.

    event based actors are the point where we start to feel actors are light weight. it is light weight because it does not have working thread wait and switched to another , working thread just switch from a piece of data work to another piece of data work, thus keep spinning on effective calculations.

    0 讨论(0)
  • 2021-02-19 19:06

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

    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.

    Source

    0 讨论(0)
提交回复
热议问题