-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathUsingExplicitReentrantLocks.java
80 lines (72 loc) · 2.01 KB
/
UsingExplicitReentrantLocks.java
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
77
78
79
80
package br.com.leonardoz.features.locks;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
/**
* Alternative mechanism for Intrinsic Locks, but with the same guarantees on
* serialization and visibility;
*
* A Lock interface with multiple implementations with some differences in
* behavior; it offers cancellation, fairness and others facilities.
*
* Fair in constructor:
*
* true: Fair Lock, newly requesting threads are queued if the lock is held.
*
* false: Unfair lock: if the lock is held, requesting threads can 'jump' the
* waiting queue.
*
*/
public class UsingExplicitReentrantLocks {
// Equivalent to Intrinsic Locks
private ReentrantLock reentrantLock = new ReentrantLock();
private boolean state;
/**
* Simplest way to use
*/
public void lockMyHearth() {
reentrantLock.lock();
try {
System.out.println("Changing stated in a serialized way");
state = !state;
System.out.println("Changed: " + state);
} finally {
reentrantLock.unlock();
}
}
/**
* Try lock - Timed and polled lock acquisition
*
* @throws InterruptedException
*/
public void lockMyHearthWithTiming() throws InterruptedException {
// Tries to acquire lock in the specified timeout
if (!reentrantLock.tryLock(1l, TimeUnit.SECONDS)) {
System.err.println("Failed to acquire the lock - it's already held.");
} else {
try {
System.out.println("Simulating a blocking computation - forcing tryLock() to fail");
Thread.sleep(3000);
} finally {
reentrantLock.unlock();
}
}
}
public static void main(String[] args) {
var executor = Executors.newCachedThreadPool();
var self = new UsingExplicitReentrantLocks();
for (int i = 0; i < 10; i++) {
executor.execute(() -> self.lockMyHearth());
}
for (int i = 0; i < 40; i++) {
executor.execute(() -> {
try {
self.lockMyHearthWithTiming();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
executor.shutdown();
}
}