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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:43:00+00:00 2026-05-24T02:43:00+00:00

What is the simplest way to implement a parallel computation (e.g. on a multiple

  • 0

What is the simplest way to implement a parallel computation (e.g. on a multiple core processor) using Java.
I.E. the java equivalent to this Scala code

val list = aLargeList
list.par.map(_*2)

There is this library, but it seems overwhelming.

  • 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-24T02:43:01+00:00Added an answer on May 24, 2026 at 2:43 am

    http://gee.cs.oswego.edu/dl/jsr166/dist/extra166ydocs/

    Don’t give up so fast, snappy! ))

    From the javadocs (with changes to map to your f) the essential matter is really just this:

    ParallelLongArray a = ... // you provide
    a.replaceWithMapping (new LongOp() { public long op(long a){return a*2L;}};);
    

    is pretty much this, right?

    val list = aLargeList
    list.par.map(_*2)
    

    & If you are willing to live with a bit less terseness, the above can be a reasonably clean and clear 3 liner (and of course, if you reuse functions, then its the same exact thing as Scala – inline functions.):

    ParallelLongArray a = ... // you provide
    LongOp f = new LongOp() { public long op(long a){return a*2L;}};
    a.replaceWithMapping (f);
    

    [edited above to show concise complete form ala OP’s Scala variant]

    and here it is in maximal verbose form where we start from scratch for demo:

    import java.util.Random;
    import jsr166y.ForkJoinPool;
    import extra166y.Ops.LongGenerator;
    import extra166y.Ops.LongOp;
    import extra166y.ParallelLongArray;
    
    public class ListParUnaryFunc {
        public static void main(String[] args) {
    
            int n = Integer.parseInt(args[0]);
            // create a parallel long array 
            // with random long values
            ParallelLongArray a =  ParallelLongArray.create(n-1, new ForkJoinPool());
            a.replaceWithGeneratedValue(generator);
    
            // use it: apply unaryLongFuncOp in parallel 
            //         to all values in array
            a.replaceWithMapping(unaryLongFuncOp);
    
            // examine it
            for(Long v : a.asList()){
                System.out.format("%d\n", v);
            }
        }
    
        static final Random rand = new Random(System.nanoTime());
        static LongGenerator generator = new LongGenerator() {
            @Override final
            public long op() { return rand.nextLong(); }
        };
    
        static LongOp unaryLongFuncOp = new LongOp() {
            @Override final public long op(long a) { return a * 2L; }
        };
    }
    

    Final edit and notes:

    Also note that a simple class such as the following (which you can reuse across your projects):

    /**
     * The very basic form w/ TODOs on checks, concurrency issues, init, etc.
     */
    final public static class ParArray {
        private ParallelLongArray parr;
        private final long[] arr;
        public ParArray (long[] arr){
            this.arr = arr;
        }
        public final ParArray par() {
            if(parr == null)
                parr = ParallelLongArray.createFromCopy(arr, new ForkJoinPool()) ;
            return this;
        }
        public final ParallelLongArray map(LongOp op) {
            return parr.replaceWithMapping(op);
        }
        public final long[] values() { return parr.getArray(); }
    }
    

    and something like that will allow you to write more fluid Java code (if terseness matters to you):

    long[] arr = ... // you provide
    LongOp f = ... // you provide
    
    ParArray list = new ParArray(arr);
    list.par().map(f);
    

    And the above approach can certainly be pushed to make it even cleaner.

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

Sidebar

Related Questions

I'm looking for the simplest, most straightforward way to implement the following: The main
What is the simplest way (least error-prone, least lines of code, however you want
What is the simplest way to implement a remote FIFO queue as a Python
What is the simplest/cleanest way to implement the singleton pattern in JavaScript?
I was wondering what the simplest way would be to implement an array who's
What is the simplest way of testing if an object implements a given interface
The simplest way to deal with python package installations, so far, to me, has
What's the simplest way to connect and query a database for a set of
What's the simplest way of blocking a thread until a file has been unlocked
What would be the simplest way to daemonize a python script in Linux ?

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.