Skip to content

Instantly share code, notes, and snippets.

@dweiss
Created December 11, 2020 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dweiss/4787b7aa503ef5702e94d73178c3c842 to your computer and use it in GitHub Desktop.
Save dweiss/4787b7aa503ef5702e94d73178c3c842 to your computer and use it in GitHub Desktop.
Repro for IllegalMonitorStateException in ArrayBlockingQueue
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.*;
import java.util.concurrent.atomic.*;
public class Repro {
public static void main(String[] args) throws Exception {
ArrayBlockingQueue<Object> q = new ArrayBlockingQueue<>(1);
ForkJoinPool fj =
new ForkJoinPool(
2,
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null,
true,
0,
5,
1,
null,
60_000L,
TimeUnit.MILLISECONDS);
AtomicBoolean stop = new AtomicBoolean();
for (int i = 0; i < 100; i++) {
int j = i;
fj.submit(
() -> {
try {
q.put(j);
} catch (IllegalMonitorStateException e) {
// SHOULD NEVER HAPPEN? But it will.
if (!stop.getAndSet(true)) {
e.printStackTrace();
System.out.println("fj state: " + fj);
System.exit(0);
}
} catch (InterruptedException e) {
}
});
}
for (int i = 0; i < 100; i++) {
System.out.println(": " + q.take() + " " + fj);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment