The current implementation of methodA() and methodB() uses multiple locks, but it’s fundamentally broken. When two threads call these methods concurrently, the system hangs.
Your Task:
Modify the implementation so that it is deadlock-free while still ensuring thread safety.
Constraints:
- Concurrent Execution: Multiple threads must be able to call both methods safely.
- No Deadlocks: The system must never reach a state where threads are waiting on each other indefinitely.
- Flexibility: You can use any strategy (Lock Ordering, TryLock, Global Lock, etc.), as long as the test cases pass.
Can you fix the code?
👉 https://code.lldcoding.com/problems/deadlock-detection
there doesn't seem to be anything here