Presentation is loading. Please wait.

Presentation is loading. Please wait.

1Deadlock DeadlockedBankAccount –withdraw(){ lock.lock(); while(balance <= 0){ System.out.print("W"); // waiting for the balance to exceed 0 Thread.sleep(1000);

Similar presentations


Presentation on theme: "1Deadlock DeadlockedBankAccount –withdraw(){ lock.lock(); while(balance <= 0){ System.out.print("W"); // waiting for the balance to exceed 0 Thread.sleep(1000);"— Presentation transcript:

1 1Deadlock DeadlockedBankAccount –withdraw(){ lock.lock(); while(balance <= 0){ System.out.print("W"); // waiting for the balance to exceed 0 Thread.sleep(1000); } balance -= amount; lock.unlock(); } –deposit(){ lock.lock(); while(balance > 10000){ System.out.print("W"); // waiting for the balance to go below 10000 Thread.sleep(1000) } balance += amount; lock.unlock(); }

2 2 Output: DeadlockedBankAccount Lock obtained Current balance (d): 0.0WWWWWWW

3 3DeadlockedBankAccount2 public void deposit(double amount){ while( balance > 10000 ){ System.out.print("W"); Thread.sleep(2); } lock.lock(); balance += amount; lock.unlock(); } public void withdraw(double amount){ while( balance <= 0 ){ System.out.print("W"); Thread.sleep(2); } lock.lock(); balance -= amount; lock.unlock(); }

4 4 Lock obtained WCurrent balance (d): 0.0, New balance (d): 100.0 Lock released Lock obtained Current balance (w): 100.0, New balance (w): 0.0 Lock released Lock obtained Current balance (d): 0.0, New balance (d): 100.0 Lock released Lock obtained Current balance (w): 100.0, New balance (w): 0.0 Lock released Lock obtained Current balance (d): 0.0, New balance (d): 100.0 Lock released

5 5 Withdraw thread Deposit thread If(balance true print out “W” sleep Reaches the end of its time slice Gain a time slice Acquire a lock print(“current balance…); // 0 balance += amount; // balance --> 100 println(“new balance…); // 100 Release a lock Reaches the end of its time slice (balance=100) Gain a time slice If(balance false Withdraw money Lock obtained WCurrent balance (d): 0.0, New balance (d): 100.0 Lock released

6 6 DeadlockBankAccount2 has no deadlock problems. However, it is not completely thread safe. Why?

7 7 Withdraw thread Deposit thread If(balance true Reaches the end of its time slice Gain a time slice Acquire a lock print(“current balance…); // 0 balance += amount; // balance --> 100 println(“new balance…); // 100 Release a lock Reaches the end of its time slice (balance=100) Gain a time slice print out “W” sleep() If(balance false Withdraw money These two lines are not supposed to be executed when balance > 0.

8 8 Avoiding Deadlocks Use Condition objects –Allow a thread to temporarily release a lock so that another thread can proceed –The thread goes to the Waiting state from the Runnable state. re-acquire the lock at a later time. java.util.concurrent.locks.Condition –Obtain its instance from a lock object private ReentrantLock lock; Condition condition = lock.newCondition(); condition.await();

9 9 Runnable Blocked new New start() I/O op completion or thread sync done I/O operation or wait for thread sync (lock) Terminated Exits run() or Explicit thread termination Waiting sleep() join() wait() await() notify() notifyAll() signalAll() interruption Timed Waiting sleep() join() wait() await() notify() notifyAll() signalAll() interruption

10 10ThreadSafeBankAccount2 Condition sufficientFundsCondition = lock.newCondition(); Condition belowUpperLimitFundsCondition = lock.newCondition(); withdraw(double amount){ lock.lock(); while(balance < amount){ // waiting for the balance to exceed “amount” sufficientFundsCondition.await(); } balance -= amount; lock.unlock(); belowUpperLimitFundsCondition.signalAll(); } deposit(double amount){ lock.lock(); while(balance >= 300){ // waiting for the balance to go below the upper limit. belowUpperLimitFundsCondition.await(); } balance += amount; lock.unlock(); sufficientFundsCondition.signalAll(); }

11 11Condition await() –Until it is signaled or interrupted –Until it is signaled or interrupted, or a specified waiting time (relative time) elapsed. –Until it is signaled or interrupted, or a specified deadline (absolute time) time elapsed. signalAll() –Wakes up all the threads waiting on this condition. –There is no guarantee about which thread will acquire a lock. signal() –Wakes up one of the threads waiting on this condition. –There is no guarantee about which thread is waked up.

12 12ThreadSafeBankAccount2 Output –Lock obtained –7 (d): current balance: 0.0 –7 (d): new balance: 100.0 –Lock released –Lock obtained –8 (d): current balance: 100.0 –8 (d): new balance: 200.0 –Lock released –Lock obtained –9 (d): current balance: 200.0 –9 (d): new balance: 300.0 –Lock released –Lock obtained –10 (d): current balance: 300.0 –10 (d): await(): Balance exceeds the upper limit. –Lock obtained –11 (d): current balance: 300.0 –11 (d): await(): Balance exceeds the upper limit. –Lock obtained –12 (w): current balance: 300.0 –12 (w): new balance: 200.0 –Lock released –10 (d): new balance: 300.0 –Lock released –11 (d): await(): Balance exceeds the upper limit.

13 13 Thread Synchronization Tools Lock (or mutex) –java.util.concurrent.locks.ReentrantLock Reader-writer locks –java.util.concurrent.locks.ReentrantReadWriteLock –java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock –java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock Semaphore –java.util.concurrent.Semaphore Barrier –java.util.concurrent.CyclicBarrier

14 14 Reader-Writer Locks Special types of lock Lock –Shared data must be locked when it is being changed/written. –Is data locking necessary among read-only threads? No. Read-only threads do not have to compete against each other to acquire a lock. Why not being nice or optimistic about data locking for readers?

15 15ReadWriteLock ReentrantReadWriteLock –In java.util.concurrent.locks ReentrantReadWriteLock{ public ReentrantReadWriteLock.ReadLock readLock(){} public ReentrantReadWriteLock.WriteLock writeLock(){} } –Provides two locks; a lock for writers, and the other for readers. Readers obtain a read lock. Writers obtain a write lock.

16 16 Readers can acquire a read lock even if it is already held by another reader, –AS FAR AS no writers holds a write lock. Writers can acquire a write lock ONLY IF no other writers and readers hold read/write locks. A thread trying to acquire a lock Other threads exist holding …? ReadLock WriteLock ReadLockWriteLock Y N N N

17 17 ReentrantReadWriteLock.ReadLock ReentrantReadWriteLock.WriteLock –Inner classes of ReentrantReadWriteLock They are used only by ReentrantReadWriteLock; they should not be open/available for anything else. –Work similarly to ReentrantLock. These locks support nested locking. A condition object is returned when calling newCondition() on a write lock. Calling newCondition() on a read lock generates an UnsupportedOperationException.

18 18 Sample Code ThradSafeBankAccount3 –43 msec ThradSafeBankAccount4 –33 msec 23% (10/43) faster

19 19 When to Use ReadWriteLock? When many readers run. When readers run more frequently than writers. When read operations take a long time.


Download ppt "1Deadlock DeadlockedBankAccount –withdraw(){ lock.lock(); while(balance <= 0){ System.out.print("W"); // waiting for the balance to exceed 0 Thread.sleep(1000);"

Similar presentations


Ads by Google