Skip to content

Instantly share code, notes, and snippets.

@thobson
Last active September 14, 2020 03:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save thobson/a49c896f1c6c1c4cbb58 to your computer and use it in GitHub Desktop.
Save thobson/a49c896f1c6c1c4cbb58 to your computer and use it in GitHub Desktop.
A simple example which shows how to work around the common Fork Join Pool in Java 8
public class ForkJoinTaskPoolTest {
public static void main(String args[]) throws Exception {
ThreadTest test = new ThreadTest();
test.executeTasks();
}
void executeTasks() throws Exception {
final List<Integer> firstRange = buildIntRange();
final List<Integer> secondRange = buildIntRange();
ForkJoinPool forkJoinPool = new ForkJoinPool(4);
forkJoinPool.submit(() -> {
firstRange.parallelStream().forEach((number) -> {
try {
Thread.sleep(5);
} catch (InterruptedException e) { }
});
});
ForkJoinPool forkJoinPool2 = new ForkJoinPool(4);
forkJoinPool2.submit(() -> {
secondRange.parallelStream().forEach((number) -> {
try {
Thread.sleep(5);
} catch (InterruptedException e) {
}
});
});
Thread.sleep(20_000);
}
private List<Integer> buildIntRange() {
List<Integer> numbers = new ArrayList<>(5);
for (int i=0; i<60_000; i++)
numbers.add(i);
return Collections.unmodifiableList(numbers);
}
}
@xiaozhiliaoo
Copy link

main method should be ForkJoinTaskPoolTest test = new ForkJoinTaskPoolTest();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment