score:9
it is true that for a pure event-based actor, its reacting code runs on the same thread as message sending code.
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
score:3
the scheduler library uses a thread pool to control execution of the actors. i don't know the specifics of the logic it uses, but, to me, it would seem natural to expect it to:
initialize with more than one thread in the pool, as multithreaded applications are very likely to use more than one thead.
select the thread to be used with a waiting actor in a queue-like manner -- threads are freed to the end of the queue, and acquire from the beginning of the queue.
also, i assume some threads are used to handle the scheduling itself as well as message passing.
score:4
to see the effect described in the previous answers, you need to generate more than two threads. this sample program generates 100 threads with receive and 100 threads with react.
when you run it, you can see that the receive actors each occupy a thread, and the react ones share a small number of threads. (it's easiest to see when you sort the output.)
import scala.actors._
import scala.actors.actor._
class reactactor extends actor {
def act {
loop {
react {
case 'hello => println("react: " + thread.currentthread)
}
}
}
}
class receiveactor extends actor {
def act {
while (true) {
receive {
case 'hello => println("receive: " + thread.currentthread)
}
}
}
}
object main {
def main(args: array[string]) {
val count = 100
val as = new array[actor](2 * count)
for (i <- 0 until count) {
as(i) = new reactactor
as(count + i) = new receiveactor
}
for (a <- as) a.start()
actor {
println(thread.currentthread)
for (a <- as) a ! 'hello
}
}
}
the sorted output in a typical program run:
thread[thread-102,5,main]
react: thread[thread-102,5,main]
react: thread[thread-102,5,main]
react: thread[thread-102,5,main]
...
react: thread[thread-102,5,main]
react: thread[thread-103,5,main]
react: thread[thread-103,5,main]
...
react: thread[thread-103,5,main]
react: thread[thread-104,5,main]
react: thread[thread-104,5,main]
react: thread[thread-104,5,main]
...
react: thread[thread-104,5,main]
react: thread[thread-105,5,main]
react: thread[thread-105,5,main]
react: thread[thread-105,5,main]
...
react: thread[thread-105,5,main]
receive: thread[thread-1,5,main]
receive: thread[thread-10,5,main]
receive: thread[thread-100,5,main]
receive: thread[thread-101,5,main]
receive: thread[thread-11,5,main]
receive: thread[thread-12,5,main]
receive: thread[thread-13,5,main]
receive: thread[thread-14,5,main]
receive: thread[thread-15,5,main]
receive: thread[thread-16,5,main]
receive: thread[thread-17,5,main]
receive: thread[thread-18,5,main]
receive: thread[thread-19,5,main]
receive: thread[thread-2,5,main]
receive: thread[thread-20,5,main]
receive: thread[thread-21,5,main]
...
score:5
don't assume a separate thread per actor. the scala machinery creates a pool of worker threads and only grows that pool if the size of 'blocked' actors is greater than the pool size. when your actor calls receive
, it's in a blocked state until it receives its message.
Source: stackoverflow.com
Related Query
- When are threads created for Scala actor's reacts?
- scala akka - Number of threads grows indefinitly even when actors are killed
- Injecting an ActorSystem, with actors created by Guice, in Play 2.4 for Scala
- When using the gson library for converting a Scala class to a string, UTF-8 characters are not escaped
- What are the precise rules for when you can omit parenthesis, dots, braces, = (functions), etc.?
- How are the multiple Actors implementation in Scala different?
- What are views for collections and when would you want to use them?
- How are Java threads heavy compared to Scala / Akka actors?
- Are there any tools for performing static analysis of Scala code?
- what are the options for hadoop on scala
- application.conf for Scala SBT Akka Actors
- What are possible reasons for receiving TimeoutException: Futures timed out after [n seconds] when working with Spark
- What are the use cases for Scala 2.9's try...catch generalization?
- scala actors vs threads and blocking IO
- Why doesn't Scala fully infer type parameters when type parameters are nested?
- If Singletons are so bad, why does Scala have language support for them?
- How do I set the default number of threads for Scala 2.10 parallel collections?
- prompt for user input when running scala program with sbt
- What are the best practices to deploy and host artifacts for a Docker Multicontainer environment in Elasticbeanstalk for Scala Apps?
- re-run with -feature for details, How to see scala feature warnings when building with gradle?
- For Scala are there any advantages to type erasure?
- Are Scala "continuations" just a funky syntax for defining and using Callback Functions?
- When does Scala need parameter types for anonymous and expanded functions?
- Why are Scala "for loop comprehensions" so very slow compared to FOR loops?
- What are the guarantees for scala access qualifiers?
- Idiomatic Scala for applying functions in a chain if Option(s) are defined
- Scala 2.7.x type mismatch error when passing null for a reference type
- What are the obstacles for Scala having "const classes" a la Fantom?
- Looking for examples of how to use "@_*" when doing pattern matching in Scala
- Is the Akka Actors library installed with the Scala IDE for Scala 2.10?
More Query from same tag
- Yet another "Unable to instantiate activity ComponentInfo" with Scala
- dbutils.notebook.run not working for mapping arguments
- sbt lock SNAPSHOT dependency
- ReactiveMongo mapping from ObjectId to case class BSONObjectID
- mocking methods which use ClassTag in scala using scalamock
- Scala: how to write a tail-recursive function more idiomatically
- Symbol 'type shapeless.LeftFolder' is missing from the classpath
- Return Try or Future As Generic Container
- high performance write from tcp socket to file with scala&akka
- Scala: map+filter instead of foldLeft
- Why doesn't scala.collection.Map have sort methods?
- Partition RDD in Apache Spark such that one partition consists on one file
- Can I "Pimp my Library" on a parameterized trait with method returning this.type?
- Spark Streaming (Spark 1.6) vs Structured Streaming (Spark 2.2)
- Split RDD into RDD's with no repeating values
- Why "++" works but "::" doesn't work even though they have same logic in scala
- Two threads accessing the same file in scala
- Configure working directory of Scala worksheet
- How to change JavaFX slider thumb color without CSS
- Redirect in Play after POST Request to Play 2.3.x
- Why is pattern matching ok in scala but using instanceof is a sign of bad code in Java
- Can't get path to resource
- Is there are way to create method level constants without namespace polution?
- Scala - read JSON file as a single String with Spark
- How to check if a None exists in a List of Future[Option]
- How to bundle many files in S3 using Spark
- Extract AWS Glue credentials from created glue client Scala
- Parsing a connection string with a port
- Meaning of `::` in Type Parameter?
- Scala accept only String or Int generic case class in List