读写锁
PPG007 ... 2021-12-28 Less than 1 minute
# 读写锁
ReadWriteLock 维护一对关联的 locks ,一个用于只读操作,一个用于写入。 read lock 可以由多个阅读器线程同时进行,只要没有作者。 write lock 是独家的。
public class ReadWriteLockDemo {
public static void main(String[] args) {
Cache cache = new Cache();
for (int i = 0; i < 10; i++) {
int finalI = i;
new Thread(()->{
cache.put(""+ finalI,""+ finalI);
},String.valueOf(i)).start();
}
for (int i = 0; i < 10; i++) {
int finalI = i;
new Thread(()->{
cache.read(""+ finalI);
},String.valueOf(i)).start();
}
}
}
class Cache{
private volatile HashMap<String, String> hashMap=new HashMap<>();
// 自定义公平性
private ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true);
public void put(String key,String value){
// 写锁
readWriteLock.writeLock().lock();
try {
System.out.println(Thread.currentThread().getName()+"写入"+key);
hashMap.put(key, value);
System.out.println(Thread.currentThread().getName()+"写入完成");
}catch (Exception e){
e.printStackTrace();
}finally {
// 放开写锁
readWriteLock.writeLock().unlock();
}
}
public void read(String key){
// 读锁
readWriteLock.readLock().lock();
try {
System.out.println(Thread.currentThread().getName()+"读取"+key);
System.out.println(hashMap.get(key));
System.out.println(Thread.currentThread().getName()+"读取完成");
}catch (Exception e){
e.printStackTrace();
}finally {
// 放开读锁
readWriteLock.readLock().unlock();
}
}
}
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
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