I have created the below program into which I am trying to pass the custom fork join pool and I do not want to use the common join pool but still I see that common pool is being used even after passing the fork join pool please explain why it is happening
package com.example.javanewfeatures;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ForkJoinPoolExample {
public static void main(String args[]) throws InterruptedException {
List<Integer> numbers = buildIntRange();
ForkJoinPool forkJoinPool = new ForkJoinPool(4);
Thread t1 = new Thread(() -> forkJoinPool.submit(() -> {
numbers.parallelStream().forEach(n -> {
try {
Thread.sleep(5);
System.out.println("Loop 1 : " + Thread.currentThread());
} catch (InterruptedException e) {
}
});
}).invoke());
ForkJoinPool forkJoinPool2 = new ForkJoinPool(4);
Thread t2 = new Thread(() -> forkJoinPool2.submit(() -> {
numbers.parallelStream().forEach(n -> {
try {
Thread.sleep(5);
System.out.println("Loop 2 : " + Thread.currentThread());
} catch (InterruptedException e) {
}
});
}).invoke());
t1.start();
t2.start();
t1.join();
t2.join();
}
private static List<Integer> buildIntRange() {
return IntStream.range(0, 10).boxed().collect(Collectors.toUnmodifiableList());
}
}