-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadDemo4.java
More file actions
51 lines (44 loc) · 1.01 KB
/
Copy pathThreadDemo4.java
File metadata and controls
51 lines (44 loc) · 1.01 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
package thirdPart;
/**
* @author yangxp
* @date 2017年8月8日 下午3:51:38
* 当 wait() 和 interrupt()方法相遇
*/
public class ThreadDemo4 {
public static void main(String[] args) {
try {
Object lock = new Object();
ThreadDemo4Test threadDemo4Test = new ThreadDemo4Test(lock);
threadDemo4Test.start();
Thread.sleep(2000);
threadDemo4Test.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Service4 {
public void testMethod(Object lock) {
try {
synchronized (lock) {
System.out.println("begin wait()");
lock.wait();
System.out.println("end wait()");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class ThreadDemo4Test extends Thread {
public Object lock;
public ThreadDemo4Test(Object lock){
this.lock = lock;
}
@Override
public void run() {
Service4 service4 = new Service4();
service4.testMethod(lock);
}
}