-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadWriteLockDemo.java
More file actions
76 lines (66 loc) · 1.88 KB
/
ReadWriteLockDemo.java
File metadata and controls
76 lines (66 loc) · 1.88 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
66
67
68
69
70
71
72
73
74
75
76
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ReadWriteLockDemo extends ConcurrentUtils {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(2);
Map<String, String> map = new HashMap<>();
/*
* ReadWriteLock specifies another type of lock maintaining a pair of
* locks for read and write access. read-lock can be held simultaneously
* by multiple threads as long as no threads hold the write-lock
*/
ReadWriteLock lock = new ReentrantReadWriteLock();
executor.submit(() ->
{
lock.writeLock().lock();
try {
sleep(2);
map.put("foo", "bar");
} finally {
lock.writeLock().unlock();
}
});
/*
*
*/
Runnable readTask = () -> {
//sleep(3);
lock.readLock().lock();
try {
System.out.println(map.get("foo"));
sleep(1);
} finally {
lock.readLock().unlock();
}
};
/*
* both read tasks have to wait the whole second
* until the write task has finished.
* After the write lock has been released
* both read tasks are executed in parallel
* and print the result simultaneously to the console.
* They don't have to wait for each other to finish
* because read-locks can safely be acquired concurrently
* as long as no write-lock is held by another thread.
*/
executor.submit(readTask);
executor.submit(readTask);
//writeLock will block if either readLock or writeLock is locked
executor.submit(() ->
{
lock.writeLock().lock();
try {
sleep(2);
map.put("foo", "bar2");
} finally {
lock.writeLock().unlock();
}
});
executor.submit(readTask);
stop(executor);
}
}