forked from lihengming/java-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditionTest.java
More file actions
47 lines (40 loc) · 1.15 KB
/
Copy pathConditionTest.java
File metadata and controls
47 lines (40 loc) · 1.15 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
package juc.lock;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Created by 李恒名 on 2017/6/18.
*/
public class ConditionTest {
private final Lock lock = new ReentrantLock();
private final Condition condition = lock.newCondition();
public void work() {
lock.lock();
try {
try {
System.out.println("Begin Work");
condition.await();
System.out.println("Begin End");
} catch (InterruptedException e) {
e.printStackTrace();
}
} finally {
lock.unlock();
}
}
public void continueWork() {
lock.lock();
try {
condition.signalAll();
} finally {
lock.unlock();
}
}
public static void main(String[] args) throws InterruptedException {
ConditionTest test = new ConditionTest();
new Thread(() -> test.work()).start();
//等待3000毫秒后唤醒,继续工作。
Thread.sleep(3000);
test.continueWork();
}
}