Skip to content

Instantly share code, notes, and snippets.

@dimaqq
Created November 14, 2025 05:08
Show Gist options
  • Select an option

  • Save dimaqq/812d3dbea7a909fc375609d08324f458 to your computer and use it in GitHub Desktop.

Select an option

Save dimaqq/812d3dbea7a909fc375609d08324f458 to your computer and use it in GitHub Desktop.
public class DeadlockDemo {
static class MonitorA {
synchronized void callB(MonitorB b) {
String t = Thread.currentThread().getName();
Thread.sleep(100);
b.fromA();
}
synchronized void fromB() {
String t = Thread.currentThread().getName();
}
}
static class MonitorB {
synchronized void callA(MonitorA a) {
String t = Thread.currentThread().getName();
Thread.sleep(100);
a.fromB();
}
synchronized void fromA() {
String t = Thread.currentThread().getName();
}
}
public static void main(String[] args) throws Exception {
MonitorA a = new MonitorA();
MonitorB b = new MonitorB();
Thread t1 = new Thread(() -> a.callB(b), "T1");
Thread t2 = new Thread(() -> b.callA(a), "T2");
t1.start();
t2.start();
t1.join(2000);
t2.join(2000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment