Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: Why are Actors lightweight?

Tags:

scala

actor

What makes actors so lightweight?

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

like image 235
someguy Avatar asked Mar 13 '10 17:03

someguy


1 Answers

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

like image 136
ChaosPandion Avatar answered Nov 16 '22 03:11

ChaosPandion