-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBank.java
More file actions
38 lines (34 loc) · 1.24 KB
/
Copy pathBank.java
File metadata and controls
38 lines (34 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.ddlab.rnd.reentrant;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class Bank {
private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private Lock depositLock = readWriteLock.writeLock();
private Lock withdrawLock = readWriteLock.readLock();
public void depositMoney(int amount) {
try {
depositLock.lock();
System.out.println(Thread.currentThread().getName() + " depositing money of Rs " + amount);
TimeUnit.SECONDS.sleep(10);
} catch (Exception e) {
e.printStackTrace();
} finally {
depositLock.unlock();
}
System.out.println(Thread.currentThread().getName() + " deposited money ...");
}
public void withdrawCash(int amount) {
try {
withdrawLock.lock();
System.out.println(Thread.currentThread().getName() + " withdrawing money of Rs " + amount);
TimeUnit.SECONDS.sleep(5);
} catch (Exception e) {
e.printStackTrace();
} finally {
withdrawLock.unlock();
}
System.out.println(Thread.currentThread().getName() + " withdrew money ...");
}
}