Last active
September 14, 2020 03:40
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
main method should be ForkJoinTaskPoolTest test = new ForkJoinTaskPoolTest();