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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
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:
is pretty much this, right?
& 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.):
[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:
Final edit and notes:
Also note that a simple class such as the following (which you can reuse across your projects):
and something like that will allow you to write more fluid Java code (if terseness matters to you):
And the above approach can certainly be pushed to make it even cleaner.