Close

Java 8 Streams - DoubleStream.parallel Examples

Java 8 Streams Java Java API 


Interface:

java.util.stream.DoubleStream

java.lang.AutoCloseableAutoCloseablejava.util.stream.BaseStreamBaseStreamjava.util.stream.DoubleStreamDoubleStreamLogicBig

Method:

DoubleStream parallel()

This intermediate operation returns an equivalent stream that is parallel. It may return itself, either because the stream was already parallel, or because the underlying stream state was modified to be parallel.

Examples


package com.logicbig.example.doublestream;

import java.util.stream.DoubleStream;

public class ParallelExample {

public static void main(String... args) {
System.out.println("-- parallel --");
DoubleStream ds = DoubleStream.of(1.0, 1.2, 2.0, 2.4, 3.0);
ds.parallel()
.forEach(System.out::println);

System.out.println("-- sequential --");
DoubleStream ds2 = DoubleStream.of(1.0, 1.2, 2.0, 2.4, 3.0);
ds2.forEach(System.out::println);
}
}

Output

-- parallel --
2.0
3.0
1.0
1.2
2.4
-- sequential --
1.0
1.2
2.0
2.4
3.0




package com.logicbig.example.doublestream;

import java.time.LocalTime;
import java.util.concurrent.TimeUnit;
import java.util.stream.DoubleStream;

public class ParallelExample2 {

public static void main(String... args) {
System.out.println("-- sequential --");
DoubleStream ds = DoubleStream.of(1.0, 1.2, 2.0, 2.4, 3.0);
ds.forEach(ParallelExample2::process);

System.out.println("-- parallel --");
DoubleStream ds2 = DoubleStream.of(1.0, 1.2, 2.0, 2.4, 3.0);
ds2.parallel()
.forEach(ParallelExample2::process);
}

private static void process(double v) {
try {
TimeUnit.MILLISECONDS.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.printf("%s : Process for %s, Thread name: %s, Thread count: %s%n",
LocalTime.now(), v, Thread.currentThread().getName(),
Thread.activeCount());
}
}

Output

-- sequential --
11:39:07.065 : Process for 1.0, Thread name: com.logicbig.example.doublestream.ParallelExample2.main(), Thread count: 1
11:39:07.269 : Process for 1.2, Thread name: com.logicbig.example.doublestream.ParallelExample2.main(), Thread count: 1
11:39:07.470 : Process for 2.0, Thread name: com.logicbig.example.doublestream.ParallelExample2.main(), Thread count: 1
11:39:07.670 : Process for 2.4, Thread name: com.logicbig.example.doublestream.ParallelExample2.main(), Thread count: 1
11:39:07.870 : Process for 3.0, Thread name: com.logicbig.example.doublestream.ParallelExample2.main(), Thread count: 1
-- parallel --
11:39:08.073 : Process for 1.2, Thread name: ForkJoinPool.commonPool-worker-1, Thread count: 6
11:39:08.073 : Process for 2.4, Thread name: ForkJoinPool.commonPool-worker-4, Thread count: 6
11:39:08.073 : Process for 2.0, Thread name: com.logicbig.example.doublestream.ParallelExample2.main(), Thread count: 6
11:39:08.073 : Process for 3.0, Thread name: ForkJoinPool.commonPool-worker-2, Thread count: 6
11:39:08.073 : Process for 1.0, Thread name: ForkJoinPool.commonPool-worker-3, Thread count: 6




See Also