1

Possible Duplicate:
“Parallel.For” for Java?

.NET has Parallel.For which lets you run a for loop in parallel without having to play around with threads directly.

Details are here

Is there something similar in Java? I found the exact same question (Here), however it was asked before the latest version of java came out, which claims to have:

"Concurrency utilities under JSR 166"

So, did they put something in?

Community
  • 1
  • 1
Haedrian
  • 4,240
  • 2
  • 32
  • 53
  • 2
    I believe your question has been answered [here](http://stackoverflow.com/a/4010275/1221734). – George Karanikas Jul 16 '12 at 18:29
  • I foudn that question. I linked to it. I'm wondering if there's something OFFICIAL. – Haedrian Jul 16 '12 at 18:30
  • @MarkByers - I already said that myself. I linked to the exact same question up there. I'm asking if they put something in there in Java 1.7 - which occured between that post and now. – Haedrian Jul 16 '12 at 18:30
  • 1
    There is nothing officially from Oracle which says equivalent for X in .NET is Y in Java. – Peter Lawrey Jul 16 '12 at 18:31
  • Parallel processing can be done with the Fork/Join framework as indicated in one of the answers in the thread you point to. But Parallel.For seems to also use delegates (I think that's what it is called in C#), which should be introduced in Java 8 - except that they are called lambda expressions. – assylias Jul 16 '12 at 18:32

3 Answers3

3

While it could be suggested that Parallel.For is simular to ExecutorService.submit, I suspect it is not.

public static void main(String... args) throws InterruptedException {
    long start1 = System.nanoTime();
    int runs1 = 1000;
    final int[] a = new int[100];
    for (int j = 0; j < runs1; j++) {
        for (int i = 0; i < 100; i++) {
            a[i] = a[i] * a[i];
        }
    }
    long time1 = System.nanoTime() - start1;
    System.out.printf("Each loop took an average of %,d micro-seconds%n", time1 / runs1 / 1000);

    int processors = Runtime.getRuntime().availableProcessors();
    long start2 = System.nanoTime();
    ExecutorService executor = Executors.newFixedThreadPool(processors);
    for (int j = 0; j < runs1; j++) {
        for (int i = 0; i < 100; i++) {
            final int i2 = i;
            executor.submit(new Runnable() {
                public void run() {
                    a[i2] = a[i2] * a[i2];
                }
            });
        }
    }
    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.SECONDS);
    long time2 = System.nanoTime() - start2;
    System.out.printf("Parallel: Each loop took an average of %,d micro-seconds%n", time2 / runs1 / 1000);
}

prints

Each loop took an average of 2 micro-seconds
Parallel: Each loop took an average of 149 micro-seconds

This shows that in this example, using multiple threads is a very bad idea. So I would hope that the loop is the slightly more efficient

    for (int j = 0; j < runs1; j++) {
        for (int i = 0; i < processors; i++) {
            final int i2 = i;
            executor.submit(new Runnable() {
                public void run() {
                    for (int i3 = i2 * 100 / processors; i3 < (i2 + 1) * 100 / processors && i3 < 100; i3++)
                        a[i2] = a[i2] * a[i2];
                }
            });
        }
    }

prints

Parallel: Each loop took an average of 28 micro-seconds

If you consider that the code in the Runnable is not thread safe, I suspect Parallel.For does something rather different or its pretty pointless.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Fork/Join Framework should be included with Java 8, along with lambdas/closures... Just recently there was a good speech on that subject by Angelika Langer, one of Java Champions

Ivor Prebeg
  • 988
  • 6
  • 11
1

My Parallel Computing professor, Alan Kaminsky, and his herd of graduate students have written a library that has the features you need.

Javadocs, licences, downloads, and even a free book are all covered on the information page.

It's Free Software under the GNU GPL version 3.

Parallel Java 2 Library

Jerome
  • 2,059
  • 1
  • 16
  • 19
Wug
  • 12,956
  • 4
  • 34
  • 54