-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadWriteLocksExample.java
167 lines (146 loc) · 5.94 KB
/
ReadWriteLocksExample.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package advanced_locking;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ReadWriteLocksExample {
// Re-entrant locks require an explicit locking and unlocking unlike the synchronized keyword.
// Aim: implementing an inventory database using read-write lock(s).
public static final int HIGHEST_PRICE = 1000;
public static void main(String[] args) throws InterruptedException {
InventoryDatabase inventoryDatabase = new InventoryDatabase();
Random random = new Random();
for (int i = 0; i < 100000; i++) {
inventoryDatabase.addItem(random.nextInt(HIGHEST_PRICE));
}
Thread writer = new Thread(() -> {
while (true) {
inventoryDatabase.addItem(random.nextInt(HIGHEST_PRICE));
inventoryDatabase.removeItem(random.nextInt(HIGHEST_PRICE));
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
writer.setDaemon(true);
writer.start();
int numOfReaderThreads = 7;
List<Thread> readers = new ArrayList<>();
for (int readerIdx = 0; readerIdx < numOfReaderThreads; readerIdx++) {
Thread reader = new Thread(() -> {
for (int i = 0; i < 100000; i++) {
int upperBoundPrice = random.nextInt(HIGHEST_PRICE);
int lowerBoundPrice = upperBoundPrice > 0 ? random.nextInt(upperBoundPrice) : 0;
inventoryDatabase.getNumberOfItemsInPriceRange(lowerBoundPrice, upperBoundPrice);
}
});
reader.setDaemon(true);
readers.add(reader);
}
long startReadingTime = System.currentTimeMillis();
for (Thread reader : readers) {
reader.start();
}
for (Thread reader : readers) {
reader.join();
}
long endReadingTime = System.currentTimeMillis();
System.out.printf("Reading took %d ms%n", endReadingTime - startReadingTime);
}
public static class InventoryDatabase {
private final TreeMap<Integer, Integer> priceToCountMap = new TreeMap<>();
private final ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
private final Lock readLock = reentrantReadWriteLock.readLock();
private final Lock writeLock = reentrantReadWriteLock.writeLock();
public int getNumberOfItemsInPriceRange(int lowerBound, int upperBound) {
readLock.lock();
try {
Integer fromKey = priceToCountMap.ceilingKey(lowerBound);
Integer toKey = priceToCountMap.floorKey(upperBound);
if (fromKey == null || toKey == null) {
return 0;
}
NavigableMap<Integer, Integer> rangeOfPrices = priceToCountMap.subMap(fromKey, true, toKey, true);
int sum = 0;
for (int numOfItemsForPrice : rangeOfPrices.values()) {
sum += numOfItemsForPrice;
}
return sum;
} finally {
readLock.unlock();
}
}
public void addItem(int price) {
writeLock.lock();
try {
priceToCountMap.merge(price, 1, Integer::sum);
/*
Integer numOfItemsForPrice = priceToCountMap.get(price);
if (numOfItemsForPrice == null) {
priceToCountMap.put(price, 1);
} else {
priceToCountMap.put(price, numOfItemsForPrice + 1);
}
*/
} finally {
writeLock.unlock();
}
}
public void removeItem(int price) {
writeLock.lock();
try {
Integer numOfItemsForPrice = priceToCountMap.get(price);
if (numOfItemsForPrice == null || numOfItemsForPrice == 1) {
priceToCountMap.remove(price);
} else {
priceToCountMap.put(price, numOfItemsForPrice - 1);
}
} finally {
writeLock.unlock();
}
}
/* Code below is using re-entrant lock for both read & write operations - slow
private final ReentrantLock lock = new ReentrantLock();
public int getNumberOfItemsInPriceRange(int lowerBound, int upperBound) {
lock.lock();
try {
Integer fromKey = priceToCountMap.ceilingKey(lowerBound);
Integer toKey = priceToCountMap.floorKey(upperBound);
if (fromKey == null || toKey == null) {
return 0;
}
NavigableMap<Integer, Integer> rangeOfPrices = priceToCountMap.subMap(fromKey, true, toKey, true);
int sum = 0;
for (int numOfItemsForPrice : rangeOfPrices.values()) {
sum += numOfItemsForPrice;
}
return sum;
} finally {
lock.unlock();
}
}
public void addItem(int price) {
lock.lock();
try {
priceToCountMap.merge(price, 1, Integer::sum);
} finally {
lock.unlock();
}
}
public void removeItem(int price) {
lock.lock();
try {
Integer numOfItemsForPrice = priceToCountMap.get(price);
if (numOfItemsForPrice == null || numOfItemsForPrice == 1) {
priceToCountMap.remove(price);
} else {
priceToCountMap.put(price, numOfItemsForPrice - 1);
}
} finally {
lock.unlock();
}
}
*/
}
}