-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRWLock.java
More file actions
65 lines (52 loc) · 1.46 KB
/
RWLock.java
File metadata and controls
65 lines (52 loc) · 1.46 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
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
package com.cpucode.java.aqs;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* @author : cpucode
* @date : 2021/7/30
* @time : 15:22
* @github : https://github.com/CPU-Code
* @csdn : https://blog.csdn.net/qq_44226094
*/
public class RWLock {
static Map<Integer,Object> cacheMap = new HashMap<>();
static ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
static Lock read = rwl.readLock();
static Lock write = rwl.writeLock();
public static final Object get(Integer key) {
System.out.println("开始读取数据");
// 读锁
read.lock();
try {
return cacheMap.get(key);
}finally {
read.unlock();
}
}
public static final Object put(Integer key,Object value){
System.out.println("开始写数据");
write.lock();
try{
return cacheMap.put(key, value);
}finally {
write.unlock();
}
}
public static void main(String[] args) {
for (int i = 0; i < 11; i++) {
int finalI = i;
new Thread(()->{
put(finalI, finalI);
}).start();
new Thread(()->{
get(finalI);
}).start();
}
//读->读是可以共享
//读->写 互斥
//写->写 互斥
//读多写少的场景
}
}