Java

Page 295

The problem with the LazyTeller class is that it does not consider the possibility of a race condition, a common occurrence in multithreaded programming. After the two threads are started, teller1 grabs the checking lock and teller2 grabs the savings lock. When teller1 tries to obtain the savings lock, it is not available. Therefore, teller1 blocks until the savings lock becomes available. When the teller1 thread blocks, teller1 still has the checking lock and does not let it go. Similarly, teller2 is waiting for the checking lock, so teller2 blocks but does not let go of the savings lock. This leads to one result: deadlock!

Deadlock Solution Example: Here, transfer() method, in a class named OrderedTeller, in stead of arbitrarily synchronizing on locks, this transfer() method obtains locks in a specified order based on the number of the bank account. // File Name ThreadSafeBankAccount.java publicclassThreadSafeBankAccount { privatedouble balance; privateint number; publicThreadSafeBankAccount(int num,double initialBalance) { balance = initialBalance; number = num; } publicint getNumber() { return number; } publicdouble getBalance() { return balance; } publicvoid deposit(double amount) { synchronized(this) { double prevBalance = balance; try { Thread.sleep(4000); }catch(InterruptedException e) {} balance = prevBalance + amount; } } publicvoid withdraw(double amount) { synchronized(this) { double prevBalance = balance; try { Thread.sleep(4000); }catch(InterruptedException e) {} balance = prevBalance - amount; } } } // File Name OrderedTeller.java

TUTORIALS POINT Simply Easy Learning


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.