-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBooleanLockTest3.java
More file actions
48 lines (42 loc) · 1.34 KB
/
BooleanLockTest3.java
File metadata and controls
48 lines (42 loc) · 1.34 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
package chapter05;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import static java.util.concurrent.ThreadLocalRandom.current;
/**
* Created by zhangzhao on 2018/8/26.
*/
// 阻塞的线程可超时()
public class BooleanLockTest3 {
//定义BooleanLock
private final Lock lock = new BooleanLock();
public void syncMethodTimeoutable() {
//加锁
try {
lock.lock(3000);
int randomInt = current().nextInt(10);
System.out.print(Thread.currentThread() + " get the lock. ");
TimeUnit.SECONDS.sleep(randomInt);
} catch (InterruptedException | TimeoutException e) { // 这种写法
e.printStackTrace();
} finally {
//释放锁
lock.unlock();
}
}
public static void main(String[] args){
BooleanLockTest3 blt = new BooleanLockTest3();
new Thread(blt::syncMethodTimeoutable,"T1").start();
try {
TimeUnit.MILLISECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t2 = new Thread(blt::syncMethodTimeoutable,"T2");
t2.start();
try {
TimeUnit.MILLISECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}