Repository navigation
Expand file tree
/
Copy pathTryLock.java
More file actions
63 lines (48 loc) · 1.84 KB
/
Copy pathTryLock.java
File metadata and controls
63 lines (48 loc) · 1.84 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
package modern.challenge;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;
public class TryLock {
private static final Logger logger
= Logger.getLogger(TryLock.class.getName());
private static final ReentrantLock lock = new ReentrantLock();
public boolean execute() throws InterruptedException {
if (!lock.tryLock(3, TimeUnit.SECONDS)) {
logger.info(() -> "Acquiring the lock attempt failed via "
+ Thread.currentThread().getName());
return false;
} else {
logger.info(() -> "The lock was successfully acquired by "
+ Thread.currentThread().getName());
}
try {
logger.info(() -> "Thread " + Thread.currentThread().getName()
+ " started its execution ...");
Thread.sleep(15000);
logger.info(() -> "Thread " + Thread.currentThread().getName()
+ " finished its execution ...");
} finally {
lock.unlock();
}
return true;
}
public static void main(String[] args) throws InterruptedException {
System.setProperty("java.util.logging.SimpleFormatter.format",
"[%1$tT] [%4$-7s] %5$s %n");
TryLock tryLock = new TryLock();
Runnable task = () -> {
try {
while (!tryLock.execute()) {}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
logger.severe(() -> "Exception: " + ex);
}
};
Thread t1 = new Thread(task, "t1");
t1.start();
// give time to thread 't1' to acquire the lock
Thread.sleep(1000);
Thread t2 = new Thread(task, "t2");
t2.start();
}
}