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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:00:08+00:00 2026-05-13T11:00:08+00:00

Despite the upcoming java 7 standard fork/join framework, I am building some helper method

  • 0

Despite the upcoming java 7 standard fork/join framework, I am building some helper method that is light weight in syntax for client to run code in parallel.
Here is a runnable main method to illustrate the idea.

import actors.Futures

object ForkTest2 {



  def main(args: Array[String]) {
    test1
    test2
  }



  def test1 {
    val (a, b, c) =fork({
      Thread.sleep(500)
      println("inside fx1 ",+System.currentTimeMillis)
      true
    }, {
      Thread.sleep(1000)
      println("inside fx2 ",+System.currentTimeMillis)
      "stringResult"
    }, {
      Thread.sleep(1500)
      println("inside fx3 ",+System.currentTimeMillis)
      1
    })

    println(b, a, c)
    true
  }

  def test2 {
    val results = forkAll({
      () =>
              Thread.sleep(500)
              println("inside fx1 ",+System.currentTimeMillis)
              true
    }, {
      () =>
              Thread.sleep(1000)
              println("inside fx2 ",+System.currentTimeMillis)
              "stringResult"
    }, {
      () =>
              Thread.sleep(1500)
              println("inside fx3 ",+System.currentTimeMillis)
              1
    }, {
      () =>
              Thread.sleep(2000)
              println("inside fx4 ",+System.currentTimeMillis)
              1.023
    })

    println(results)
    true
  }

  val tenMinutes = 1000 * 60 * 10

  def fork[A, B, C](
          fx1: => A,
          fx2: => B,
          fx3: => C
          ) = {
    val re1 = Futures.future(fx1)
    val re2 = Futures.future(fx2)
    val re3 = Futures.future(fx3)
    //default wait 10 minutes
    val result = Futures.awaitAll(tenMinutes, re1, re2, re3)
    (
            result(0).asInstanceOf[Option[A]],
            result(1).asInstanceOf[Option[B]],
            result(2).asInstanceOf[Option[C]]

            )
  }

  type fxAny = () => Any

  def forkAll(
          fx1: fxAny*
          ): List[Any] = {
    val results = fx1.toList.map {fx: fxAny => Futures.future(fx())}
    Futures.awaitAll(tenMinutes, results: _*)
  }
}

a sample out put is

(inside fx1 ,1263804802301)
(inside fx2 ,1263804802801)
(inside fx3 ,1263804803301)
(Some(stringResult),Some(true),Some(1))
(inside fx1 ,1263804803818)
(inside fx2 ,1263804804318)
(inside fx3 ,1263804804818)
(inside fx4 ,1263804805318)
List(Some(true), Some(stringResult), Some(1), Some(1.023))

test 1 illustrate a type safe return type

test 2 illustrate a arbitrary input argument

I hope to combine the two test method so the client code can run arbitrary function in parallel with type safe return type.

Another point about the arbitrary function arguments is:

I think the line

  type fxAny = () => Any

should really be code as

  type fxAny =  => Any

, but the scala compiler do not allow me to do so.

Any help is appreciate.

  • 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-13T11:00:08+00:00Added an answer on May 13, 2026 at 11:00 am

    Eric Torreborre wrote in the link provided by @retronym:

    trait LazyParameters { 
      /** transform a value to a zero-arg function returning that value */ 
      implicit def toLazyParameter[T](value: =>T) = new LazyParameter(() => value) 
      /** class holding a value to be evaluated lazily */ 
      class LazyParameter[T](value: ()=>T) { 
        lazy val v = value() 
        def apply() = v 
      } 
    } 
    

    Here’s LazyParameter version of your test:

    object ForkTest2 extends LazyParameters {
    

    …

    def forkAll(fx1: LazyParameter[Any]*): List[Any] = {
      val results = fx1.toList.map {
        fx: LazyParameter[Any] => Futures.future(fx.apply())}
      Futures.awaitAll(tenMinutes, results: _*)
    }
    

    Edit: As you’ve noticed, implicit evaluates the by-name parameter and it doesn’t carry forward the evaluation delay. Why not just use the word future? I personally think it makes the code more readable.

    import actors.Futures
    import actors.Futures.future
    import actors.Future
    

    …

    def test2 {
      val results = forkAll(
        future {
          Thread.sleep(500)
          println("inside fx1 ",+System.currentTimeMillis)
          true
        },
        future {
          Thread.sleep(1000)
          println("inside fx2 ",+System.currentTimeMillis)
          "stringResult"
        },
        future {
          Thread.sleep(1500)
          println("inside fx3 ",+System.currentTimeMillis)
          1
        },
        future {
          Thread.sleep(2000)
          println("inside fx4 ",+System.currentTimeMillis)
          1.023
        })
    
      println(results)
      true
    }
    

    …

    def forkAll(futures: Future[Any]*): List[Any] = {
      println("forkAll")
      Futures.awaitAll(tenMinutes, futures: _*)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 311k
  • Answers 311k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This will happen in IE if there's a directive that… May 13, 2026 at 10:25 pm
  • Editorial Team
    Editorial Team added an answer setMethod(new Text(longStringValue)); String value = text.getValue(); If you are trying… May 13, 2026 at 10:25 pm
  • Editorial Team
    Editorial Team added an answer There is no way to do this with existing API.… May 13, 2026 at 10:25 pm

Related Questions

This is my 3rd thread concerning a blowfish problem in C#.Despite the fact I
I'm experiencing some odd behavior in Moq - despite the fact that I setup
Many Java Apps don't use anti-aliased fonts by default, despite the capability of Swing
I am trying to get a DIV element to wrap its content despite the
I need to localiza a in development app for English & Spanish. Despite the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.