* BAEL-6577 - Paralellize for loops in Java

* BAEL-6577 - Paralellize for loops in Java

* BAEL-6577 - Paralellize for loops in Java - adding JMH

* BAEL-6577 - Paralellize for loops in Java - using CompletableFuture
This commit is contained in:
Abhinav Pandey 2023-07-04 20:07:40 +05:30 committed by GitHub
parent 1c12d45700
commit 0b193f4544
2 changed files with 87 additions and 0 deletions

View File

@ -0,0 +1,14 @@
package com.baeldung.concurrent.parallel;
import java.io.IOException;
class Benchmark {
public static void main(String[] args) {
try {
org.openjdk.jmh.Main.main(new String[]{"com.baeldung.concurrent.parallel.Processor"});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,73 @@
package com.baeldung.concurrent.parallel;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Mode;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class Processor {
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public void processSerially() throws InterruptedException {
for (int i = 0; i < 100; i++) {
Thread.sleep(10);
}
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public void processParallelyWithExecutorService() throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(10);
List<CompletableFuture<Void>> futures = new ArrayList<>();
for (int i = 0; i < 100; i++) {
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}, executorService);
futures.add(future);
}
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
executorService.shutdown();
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public void processParallelyWithStream() {
IntStream.range(0, 100)
.parallel()
.forEach(i -> {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public void processParallelyWithStreamSupport() {
Iterable<Integer> iterable = () -> IntStream.range(0, 100).iterator();
Stream<Integer> stream = StreamSupport.stream(iterable.spliterator(), true);
stream.forEach(i -> {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}