Skip to content

Commit 6a87aa0

Browse files
committed
🚚 mysql DML 语句示例
1 parent 7b2fa15 commit 6a87aa0

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.github.dunwu.javase.concurrent.lock;
2+
3+
import java.util.concurrent.locks.ReentrantReadWriteLock;
4+
5+
/**
6+
* ReentrantReadWriteLock 示例
7+
* @author Zhang Peng
8+
* @date 2018/5/11
9+
*/
10+
public class ReentrantReadWriteLockDemo {
11+
12+
private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
13+
14+
public static void main(String[] args) {
15+
final ReentrantReadWriteLockDemo demo = new ReentrantReadWriteLockDemo();
16+
new Thread(() -> demo.get(Thread.currentThread())).start();
17+
new Thread(() -> demo.get(Thread.currentThread())).start();
18+
}
19+
20+
public synchronized void get(Thread thread) {
21+
rwl.readLock().lock();
22+
try {
23+
long start = System.currentTimeMillis();
24+
25+
while (System.currentTimeMillis() - start <= 1) {
26+
System.out.println(thread.getName() + "正在进行读操作");
27+
}
28+
System.out.println(thread.getName() + "读操作完毕");
29+
} finally {
30+
rwl.readLock().unlock();
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)