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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:17:11+00:00 2026-05-25T16:17:11+00:00

Input the following little sequential program and its parallelized version in Scala REPL: /*

  • 0

Input the following little sequential program and its parallelized version in Scala REPL:

/* Activate time measurement in "App" class. Prints [total <X> ms] on exit. */
util.Properties.setProp("scala.time", "true")
/* Define sequential program version. */
object X extends App { for (x <- (1 to 10)) {Thread.sleep(1000);println(x)}}
/* Define parallel program version. Note '.par' selector on Range here. */
object Y extends App { for (y <- (1 to 10).par) {Thread.sleep(1000);println(y)}}

Executing X with X.main(Array.empty) gives:

1
2
3
4
5
6
7
8
9
10
[total 10002ms]

Whereas Y with Y.main(Array.empty) gives:

1
6
2
7
3
8
4
9
10
5
[total 5002ms]

So far so good. But what about the following two variations of the program:

object X extends App {(1 to 10).foreach{Thread.sleep(1000);println(_)}}
object Y extends App {(1 to 10).par.foreach{Thread.sleep(1000);println(_)}}

The give me runtimes of [total 1002ms] and [total 1002ms] respectively. How can this be?

  • 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-25T16:17:11+00:00Added an answer on May 25, 2026 at 4:17 pm

    This have nothing to do with parallel collections. The problem is hidden in the function literal. You can see it if you let the compiler show the AST (with option -Xprint:typer):

    for (x <- (1 to 10)) {Thread.sleep(1000);println(x)}
    

    produces

    scala.this.Predef.intWrapper(1).to(10).foreach[Unit](((x: Int) => {
      java.this.lang.Thread.sleep(1000L);
      scala.this.Predef.println(x)
    }))
    

    whereas

    (1 to 10).foreach{Thread.sleep(1000);println(_)}
    

    produces

    scala.this.Predef.intWrapper(1).to(10).foreach[Unit]({
      java.this.lang.Thread.sleep(1000L);
      ((x$1: Int) => scala.this.Predef.println(x$1))
    })
    

    There is a little difference. If you want the expected result you have to change the foreach-expression to

    (1 to 10).foreach{x => Thread.sleep(1000);println(x)}
    

    But what is the difference? In your code you declare a block to foreach and after executing the block it will return the function to execute. Then this returned function is delivered to foreach and not the block which contains it.

    This mistake is often done. It has to do with the underscore literal. Maybe this question helps you.

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

Sidebar

Related Questions

Had to write the following program for an on line pre java class using,
The output I'm getting for my little example app is the following: Welcome to
I have the following input: <node TEXT=txt> <node TEXT=txt> <node TEXT=txt/> <node TEXT=txt/> </node>
Assume I have the following input in Pig: some And I would like to
For ScoreOption , I expect to get the following input A, B, and T_(state)
the following latex input: \rule{1cm}{1cm}\\ \rule{1cm}{1cm} leaves a gap of about .35mm between the
I have the following text input on a budget calculator form which displays the
I´ve got a Jquery autocomplete input like the following: $(#cities).autocomplete(regionIDs, { minChars: 2, width:
I'm trying to clean up form input using the following Perl transliteration: sub ValidateInput
I have a logical error. I provided the following as input: the salary is

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.