Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #32747

Re: parallel stream with return

From Jeff Higgins <jeff@invalid.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: parallel stream with return
Date 2015-10-01 08:54 -0400
Organization A noiseless patient Spider
Message-ID <mujaam$r5h$1@dont-email.me> (permalink)
References <e585b3de-181e-4963-8a00-341749bf00ab@googlegroups.com> <muhhb6$4fi$1@dont-email.me> <muhi14$7ep$1@dont-email.me> <20d1db19-e4b1-45ec-8acd-effcf8fac6a7@googlegroups.com>

Show all headers | View raw


On 10/01/2015 02:36 AM, Philipp Kraus wrote:
> Am Mittwoch, 30. September 2015 22:54:04 UTC+2 schrieb Jeff Higgins:
>
>>       IntStream.range(0, 4096).parallel()
>>         .forEachOrdered(i -> { x.add(generate(i)); });
>
> Did I understand It correct, that I need the forEachOrdered to create the equal order like in the intstream range? So if I need only 4k objects and the order is irrelevant I can do this with forEach.
>
> Phil
>
You can look at the documentation for Stream.forEach and
Stream.ForEachOrdered and package java.util.stream.

And you can try these experiments with the class below.

Run it as is.
Run with change : List<Scratch> list = new ArrayList<>(4096);
Run with change : private static Scratch generate(int i) {


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.IntStream;

public class Scratch {

   private final int count;
   private final String name;

   private Scratch(int i, String t) { count = i; name = t;}

   public static void main(String[] args) {
     List<Scratch> list = new ArrayList<>();
     List<Scratch> x = Collections.synchronizedList(list);
     IntStream.range(0, 4096).parallel()
       .forEach(i -> { x.add(generate(i)); });
     for (Scratch s : x) {
       System.out.println(s.count + " : " + s.name);
     }
   }

   private static synchronized Scratch generate(int i) {
     return new Scratch(i, Thread.currentThread().getName());
   }

}

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

parallel stream with return Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2015-09-29 23:35 -0700
  Re: parallel stream with return Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-09-30 08:01 -0400
    Re: parallel stream with return Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2015-09-30 08:02 -0700
      Re: parallel stream with return Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-09-30 13:22 -0400
        Re: parallel stream with return Steven Simpson <ss@my.domain.invalid> - 2015-09-30 22:25 +0100
  Re: parallel stream with return Jeff Higgins <jeff@invalid.invalid> - 2015-09-30 16:41 -0400
    Re: parallel stream with return Jeff Higgins <jeff@invalid.invalid> - 2015-09-30 16:53 -0400
      Re: parallel stream with return Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2015-09-30 23:36 -0700
        Re: parallel stream with return Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-01 08:22 -0400
        Re: parallel stream with return Jeff Higgins <jeff@invalid.invalid> - 2015-10-01 08:54 -0400

csiph-web