Backing off with tryLock
done[0]++ is safe here because it only runs while holding both locks.
A deadlock is threads waiting for each other forever, usually because they take the same locks in different orders. Livelock is threads busily reacting to each other without progress, and starvation is a thread that never gets a turn. Lock ordering, timeouts and fewer shared locks prevent them.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Explain how you'd make a transfer between two Accounts deadlock-free when accounts have no id field.
Explain it without notes
What four conditions must hold for a deadlock, and how does lock ordering prevent it?
How would you find the cause of a hung Java service?
What is livelock and how does jitter help?
Expected output
operations completed: 400