Created
November 14, 2025 05:08
-
-
Save dimaqq/812d3dbea7a909fc375609d08324f458 to your computer and use it in GitHub Desktop.
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 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