Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f95291c

Browse files
committed
add count down latch await in redisson sample
1 parent fe1263e commit f95291c

File tree

1 file changed

+8
-5
lines changed
  • springboot-redisson-sample/src/main/java/com/ipman/springboot/redisson/sample/examples

1 file changed

+8
-5
lines changed

‎springboot-redisson-sample/src/main/java/com/ipman/springboot/redisson/sample/examples/RedissonExamples.java‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,13 @@ public void map() {
109109
System.out.println("Map async put:" + futurePut.get());
110110
System.out.println("Map async get:" + futureGet.get());
111111

112+
// 阻塞等待
113+
int count = 6;
114+
CountDownLatch latch = new CountDownLatch(count);
112115

113116
// Map 对象锁,先获取锁,才能操作 Map 集合
114117
String k = "mapLock";
115-
for (int i = 0; i < 3; i++) {
118+
for (int i = 0; i < count / 2; i++) {
116119
Thread t = new Thread(() -> {
117120
RLock keyLock = map.getLock(k);
118121
keyLock.lock();
@@ -122,14 +125,15 @@ public void map() {
122125
// 其他业务逻辑
123126
} finally {
124127
keyLock.unlock();
128+
latch.countDown();
125129
}
126130
});
127131
t.setName("thread-" + i);
128132
t.start();
129133
}
130134

131135
// Map 读写锁,读时不能写
132-
for (int i = 0; i < 3; i++) {
136+
for (int i = 0; i < count / 2; i++) {
133137
Thread t = new Thread(() -> {
134138
RReadWriteLock rwLock = map.getReadWriteLock(k);
135139
rwLock.readLock().lock();
@@ -139,15 +143,14 @@ public void map() {
139143
// 其他业务逻辑
140144
} finally {
141145
rwLock.readLock().unlock();
146+
latch.countDown();
142147
}
143148
});
144149
t.setName("thread-" + i);
145150
t.start();
146151
}
147152

148-
// 阻塞等待
149-
System.in.read();
150-
153+
latch.await();
151154
}
152155

153156

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /