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 359497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:23:58+00:00 2026-05-12T12:23:58+00:00

I was wondering if there was a way to execute very simple tasks on

  • 0

I was wondering if there was a way to execute very simple tasks on another thread in scala that does not have a lot of overhead?

Basically I would like to make a global ‘executor’ that can handle executing an arbitrary number of tasks. I can then use the executor to build up additional constructs.

Additionally it would be nice if blocking or non-blocking considerations did not have to be considered by the clients.

I know that the scala actors library is built on top of the Doug Lea FJ stuff, and also that they support to a limited degree what I am trying to accomplish. However from my understanding I will have to pre-allocate an ‘Actor Pool’ to accomplish.

I would like to avoid making a global thread pool for this, as from what I understand it is not all that good at fine grained parallelism.

Here is a simple example:

import concurrent.SyncVar
object SimpleExecutor {
  import actors.Actor._
  def exec[A](task:  => A) : SyncVar[A] = {
    //what goes here?
    //This is what I currently have
    val x = new concurrent.SyncVar[A]
    //The overhead of making the actor appears to be a killer
    actor {
      x.set(task)
    }
    x
  }
  //Not really sure what to stick here
  def execBlocker[A](task: => A) : SyncVar[A] = exec(task)

}

and now an example of using exec:

object Examples {
  //Benchmarks a task
  def benchmark(blk : => Unit) = {
    val start = System.nanoTime
    blk
    System.nanoTime - start
  }

  //Benchmarks and compares 2 tasks
  def cmp(a: => Any, b: => Any) = {
    val at = benchmark(a)
    val bt = benchmark(b)
    println(at + " " + bt + " " +at.toDouble / bt)
  }

  //Simple example for simple non blocking comparison
  import SimpleExecutor._
  def paraAdd(hi: Int) = (0 until hi) map (i=>exec(i+5)) foreach (_.get)
  def singAdd(hi: Int) = (0 until hi) foreach (i=>i+5)

  //Simple example for the blocking performance
  import Thread.sleep
  def paraSle(hi : Int) = (0 until hi) map (i=>exec(sleep(i))) foreach (_.get)
  def singSle(hi : Int) = (0 until hi) foreach (i=>sleep(i))
}

Finally to run the examples (might want to do it a few times so HotSpot can warm up):

import Examples._
cmp(paraAdd(10000), singAdd(10000))
cmp(paraSle(100), singSle(100))
  • 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-12T12:23:58+00:00Added an answer on May 12, 2026 at 12:23 pm

    That’s what Futures was made for. Just import scala.actors.Futures._, use future to create new futures, methods like awaitAll to wait on the results for a while, apply or respond to block until the result is received, isSet to see if it’s ready or not, etc.

    You don’t need to create a thread pool either. Or, at least, not normally you don’t. Why do you think you do?

    EDIT

    You can’t gain performance parallelizing something as simple as an integer addition, because that’s even faster than a function call. Concurrency will only bring performance by avoiding time lost to blocking i/o and by using multiple CPU cores to execute tasks in parallel. In the latter case, the task must be computationally expensive enough to offset the cost of dividing the workload and merging the results.

    One other reason to go for concurrency is to improve the responsiveness of the application. That’s not making it faster, that’s making it respond faster to the user, and one way of doing that is getting even relatively fast operations offloaded to another thread so that the threads handling what the user sees or does can be faster. But I digress.

    There’s a serious problem with your code:

      def paraAdd(hi: Int) = (0 until hi) map (i=>exec(i+5)) foreach (_.get)
      def singAdd(hi: Int) = (0 until hi) foreach (i=>i+5)
    

    Or, translating into futures,

      def paraAdd(hi: Int) = (0 until hi) map (i=>future(i+5)) foreach (_.apply)
      def singAdd(hi: Int) = (0 until hi) foreach (i=>i+5)
    

    You might think paraAdd is doing the tasks in paralallel, but it isn’t, because Range has a non-strict implementation of map (that’s up to Scala 2.7; starting with Scala 2.8.0, Range is strict). You can look it up on other Scala questions. What happens is this:

    1. A range is created from 0 until hi
    2. A range projection is created from each element i of the range into a function that returns future(i+5) when called.
    3. For each element of the range projection (i => future(i+5)), the element is evaluated (foreach is strict) and then the function apply is called on it.

    So, because future is not called in step 2, but only in step 3, you’ll wait for each future to complete before doing the next one. You can fix it with:

      def paraAdd(hi: Int) = (0 until hi).force map (i=>future(i+5)) foreach (_.apply)
    

    Which will give you better performance, but never as good as a simple immediate addition. On the other hand, suppose you do this:

    def repeat(n: Int, f: => Any) = (0 until n) foreach (_ => f)
    def paraRepeat(n: Int, f: => Any) = 
      (0 until n).force map (_ => future(f)) foreach (_.apply)
    

    And then compare:

    cmp(repeat(100, singAdd(100000)), paraRepeat(100, singAdd(100000)))
    

    You may start seeing gains (it will depend on the number of cores and processor speed).

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

Sidebar

Related Questions

I am wondering is there any way to execute following shell script, which waits
I was wondering is there a way in PHP that you could tell where
Wondering if there is any way to get the lambda expressions that result from
Just wondering if there is a way in Spring to have a parent controller:
I was wondering if there's a better way to execute a link's href attribute
I was wondering if there is a way to execute mocha tests programmatically from
I was wondering if there is a way to execute script within a ajax
I'm wondering if there's a way in Chrome I can execute a .js file
I'm wondering, is there a way to only execute a block if no exception
I am wondering is there a way to render a partial view in the

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.