Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 250177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:29:22+00:00 2026-05-11T21:29:22+00:00

The reply to a recent question of mine indicated that an actor processed its

  • 0

The reply to a recent question of mine indicated that an actor processed its messages one at a time. Is this true? I see nothing that explicitly says that (in Programming in Scala), which contains the following snippet (pp. 593)

If [the react method] finds a message that can be handled, [it] will schedule the handling of that message for later execution and throw an exception

(Emphasis my own). Two related (and mutually exclusive) questions:

  1. Assuming an actor could process multiple messages simulatenously, how can I force an actor to process messages 1 at a time (if this is what I wish to do)? (using receive?)
  2. Assuming an actor processes messages one at a time, how would I best implement an actor which in fact could process messages concurrently

edit: doing a bit of testing seems to bear out that I am wrong and that actors are indeed sequential. So it’s question #2 which I need answering

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:29:22+00:00Added an answer on May 11, 2026 at 9:29 pm

    Actors process one message at a time. The classic pattern to process multiple messages is to have one coordinator actor front for a pool of consumer actors. If you use react then the consumer pool can be large but will still only use a small number of JVM threads. Here’s an example where I create a pool of 10 consumers and one coordinator to front for them.

    import scala.actors.Actor
    import scala.actors.Actor._
    
    case class Request(sender : Actor, payload : String)
    case class Ready(sender : Actor)
    case class Result(result : String)
    case object Stop
    
    def consumer(n : Int) = actor {
      loop {
        react {
          case Ready(sender) => 
            sender ! Ready(self)
          case Request(sender, payload) =>
            println("request to consumer " + n + " with " + payload)
            // some silly computation so the process takes awhile
            val result = ((payload + payload + payload) map {case '0' => 'X'; case '1' => "-"; case c => c}).mkString
            sender ! Result(result)
            println("consumer " + n + " is done processing " + result )
          case Stop => exit
        }
      }
    }
    
    // a pool of 10 consumers
    val consumers = for (n <- 0 to 10) yield consumer(n)
    
    val coordinator = actor {
      loop {
         react {
            case msg @ Request(sender, payload) =>
               consumers foreach {_ ! Ready(self)}
               react {
                  // send the request to the first available consumer
                  case Ready(consumer) => consumer ! msg
               }
             case Stop => 
               consumers foreach {_ ! Stop} 
               exit
         }
      }
    }
    
    // a little test loop - note that it's not doing anything with the results or telling the coordinator to stop
    for (i <- 0 to 1000) coordinator ! Request(self, i.toString)
    

    This code tests to see which consumer is available and sends a request to that consumer. Alternatives are to just randomly assign to consumers or to use a round robin scheduler.

    Depending on what you are doing, you might be better served with Scala’s Futures. For instance, if you don’t really need actors then all of the above machinery could be written as

    import scala.actors.Futures._
    
    def transform(payload : String) = {      
      val result = ((payload + payload + payload) map {case '0' => 'X'; case '1' => "-"; case c => c}).mkString
      println("transformed " + payload + " to " + result )
      result
    }
    
    val results = for (i <- 0 to 1000) yield future(transform(i.toString))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about this question . I posted a reply there but
I saw this reply from Jon on Initialize generic object with unknown type :
I've posted this on django-users but haven't received a reply! So I have my
No doubt elements of this question have been asked before, but I'm having trouble
Is it possible to return querysets that return only one object per foreign key?
In a recent conversation, I mentioned that I was using JavaScript for a web
Does anyone know of a good sample (inline in reply, or web tutorial) for
I'm refactoring some client-server code and it uses the terms Response, Result & Reply
Is there something like python's interactive REPL mode, but for Java? So that I
I don't want to rely on the one-click installer any more, and I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.