

Let’s analyze the code to understand why.In the Bank class you will each account is initialized with an amount of 100. Why can deadlock happen when we increase the maximum amount of money can be transferred among accounts? Guess what will happen?You will see that the program runs for a few transactions and hangs forever, as shown in the following screenshot: The program encounters a deadlock and cannot continue.

Modify the maximum amount can be transferred from 10 to 200 in the Bank class as follows: public static final int MAX_AMOUNT = 200 Look at the Transaction class you see the amount is chosen randomly by this statement: int amount = (int) (Math.random() * Bank.MAX_AMOUNT) Now, recompile the Bank and Transaction classes, and then run the TransactionTest program. When deadlock occurs, the program hangs forever and the only thing you can do is to kill the program.Let’s consider the account transaction example in this tutorial. Understanding Deadlock Deadlock describes a situation where two more threads are blocked because of waiting for each other forever. You will be able to identify each kind of problem so you can know to avoid them. This Java concurrency tutorial helps you understand the 3 problems that may happen in multi-threaded applications: deadlock, livelock and starvation.
