advanced
Diagnose a Concurrency Race Condition
Analyze a snippet of multi-threaded code to identify and fix a deadlock.
📝 Treść promptu
Analyze the following theoretical Java scenario: Two threads, T1 and T2, share access to resources R1 and R2. T1 locks R1 then attempts to lock R2. T2 locks R2 then attempts to lock R1. Describe the exact sequence of events that leads to a deadlock. Then, rewrite the logic pseudocode to prevent this using the 'Lock Ordering' strategy (also known as resource hierarchy solution). Additionally, explain how the 'wait/notify' mechanism could be misused here to cause 'livelock' instead of deadlock, and how to avoid it.