Last active
October 1, 2020 07:55
-
-
Save lrytz/a622c541d91336030a8cb788273bdc66 to your computer and use it in GitHub Desktop.
ForkJoinPool JDK 11+
This file contains 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
import java.util.concurrent.*; | |
public class Test { | |
static boolean triggerIssue = true; | |
static ForkJoinPool fjp; | |
static void hotSleep(int ms) { | |
long until = System.nanoTime() + ms*1000_000; | |
while (System.nanoTime() < until) { } | |
} | |
public static void main(String[] args) { | |
int parallelism = (args.length > 0) ? Integer.parseInt(args[0]) : Runtime.getRuntime().availableProcessors(); | |
fjp = new ForkJoinPool(parallelism); | |
for (int i = 0; i < 1000; i += 1) { | |
if (i % 25 == 0) | |
System.out.println("iteration " + i); | |
hotSleep(25); | |
long begin = System.nanoTime(); | |
if (!triggerIssue) | |
fjp.execute(() -> hotSleep(205)); // this doesn't trigger the issue | |
T t = new T(50); | |
fjp.execute(t); | |
t.join(); | |
long duration = (System.nanoTime() - begin) / 1000_000; | |
if (duration > 200) | |
System.out.println(" " + i + ": suspicious duration " + duration); | |
} | |
} | |
static class T extends RecursiveAction { | |
private static T[] noTs = {}; | |
int n; | |
public T(int n) { | |
this.n = n; | |
} | |
public void compute() { | |
T[] subtasks = (n > 2) ? new T[] { new T(n / 2), new T(n / 2) } : noTs; | |
for (T t : subtasks) | |
t.fork(); | |
if (triggerIssue && n == 50) | |
fjp.execute(() -> hotSleep(205)); | |
hotSleep(2); | |
for (T t : subtasks) | |
t.join(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment