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

  • Home
  • SEARCH
  • 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 6836373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:20:51+00:00 2026-05-26T23:20:51+00:00

I have read that, when using react, all actors can execute in a single

  • 0

I have read that, when using react, all actors can execute in a single thread. I often process a collection in parallel and need to output the result. I do not believe System.out.println is threadsafe so I need some protection. One way (a traditional way) I could do this:

val lock = new Object
def printer(msg: Any) {
  lock.synchronized {
    println(msg)
  }
}

(1 until 1000).par.foreach { i =>
  printer(i)
}

println("done.")

How does this first solution compare to using actors in terms of efficiency? Is it true that I’m not creating a new thread?

val printer = actor {
  loop {
    react {
      case msg => println(msg)
    }
  }
}

(1 until 10000).par.foreach { i =>
  printer ! i
}

println("done.")

It doesn’t seem to be a good alternative however, because the actor code never completes. If I put a println at the bottom it is never hit, even though it looks like it goes through every iteration for i. What am I doing wrong?

  • 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-26T23:20:52+00:00Added an answer on May 26, 2026 at 11:20 pm

    As you have it now with your Actor code, you only have one actor doing all the printing. As you can see from running the code, the values are all printed out sequentially by the Actor whereas in the parallel collection code, they’re out of order. I’m not too familiar with parallel collections, so I don’t know the performance gains between the two.

    However, if your code is doing a lot of work in parallel, you probably would want to go with multiple actors. You could do something like this:

    def printer = actor {
      loop {
        react {
          case msg => println(msg)
        }
      }
    }
    
    val num_workers = 10
    val worker_bees = Vector.fill(num_workers)(printer)
    
    (1 until 1000).foreach { i =>
        worker_bees(i % num_workers) ! i
    }
    

    The def is important. This way you’re actually creating multiple actors and not just flooding one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apparently I can't move files on different volumes using Directory.Move. I have read that
I have read that using database keys in a URL is a bad thing
I have read that to match a word inside of a string using Regular
i have read in more then one place that using NHibernate's Identifier as Primary
Throughout a Bison grammar I am using right recursion, and I have read that
I have a config file that I read using the RawConfigParser in the standard
I have a file that I want to read in using the File::Slurp module
I have read that you can do it, but would this really improve performance
I was wondering how can I read text of a control that doesn't have
I have read that it is better to select using id than class with

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.