Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: eliminated redundant interleaving para

...

The two transfers are performed in their own threads, from instance a to b and b to a. The first thread atomically transfers the amount from a to b by depositing the balance from a to b and withdrawing the entire balance from a. The second thread performs the reverse operation, that is, it transfers the balance from b to a and withdraws the balance from b. When executing depositAllAmount(), the first thread might acquire a lock on object a while the second thread may acquire a lock on object b. Subsequently, the first thread requests a lock on b which is already held by the second thread and the second thread requests a lock on a which is already held by the first thread. This constitutes a deadlock condition, as neither thread can proceed.

The threads in this program request monitors in different orders depending on the interleaving of method calls. If Thread T1 finishes executing before Thread T2, or T2 before T1, there are no issues because in these cases, locks are acquired and released in the same order. Sequences where the threads alternate, such as, T1, T2, T1, T2 may cause a deadlock.

Compliant Solution (static internal private lock)

...