-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimisticLock.java
More file actions
72 lines (62 loc) · 2.14 KB
/
OptimisticLock.java
File metadata and controls
72 lines (62 loc) · 2.14 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
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.StampedLock;
public class OptimisticLock extends ConcurrentUtils {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(2);
StampedLock lock = new StampedLock();
executor.submit(() ->
{
/*
* optimistic read lock is acquired by calling tryOptimisticRead()
* which always returns a stamp without blocking the current thread,
* no matter if the lock is actually available.
* If there's already a write lock active
* the returned stamp equals zero.
*
* The optimistic lock is valid right after acquiring the lock.
* In contrast to normal read locks
* an optimistic lock doesn't prevent other threads
* to obtain a write lock instantaneously.
* (doesn't block any thread)
*
* After sending the first thread to sleep for one second
* the second thread obtains a write lock
* without waiting for the optimistic read lock to be released.
* From this point the optimistic read lock is no longer valid.
* Even when the write lock is released
* the optimistic read locks stays invalid.
*/
long stamp = lock.tryOptimisticRead();
try {
/* Use case: after read the data, check if stamp is validate,
* if good, return data
* If not, acquire a real read lock to get data again
*/
System.out.println("Optimistic Lock Valid: "
+ lock.validate(stamp)); //true
//sleep(1);
System.out.println("Optimistic Lock Valid: "
+ lock.validate(stamp)); //false
//sleep(2);
System.out.println("Optimistic Lock Valid: "
+ lock.validate(stamp)); //false
} finally {
//For tryOptimisticRead lock, it doesn't need to unlock
lock.unlock(stamp);
}
});
executor.submit(() ->
{
long stamp = lock.writeLock();
try {
System.out.println("Write Lock acquired");
sleep(2);
} finally {
lock.unlock(stamp);
System.out.println("Write done");
}
});
stop(executor);
}
}