-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWaitAndNotify.java
More file actions
44 lines (35 loc) · 1.06 KB
/
WaitAndNotify.java
File metadata and controls
44 lines (35 loc) · 1.06 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
public class WaitAndNotify {
private static final Object lock=new Object();
public static void main(String[] args) {
Thread t1=new Thread(()->{
try {
one();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
Thread t2=new Thread(()->{
try {
two();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
t1.start();
t2.start();
}
private static void one() throws InterruptedException{
synchronized (lock){
System.out.println("Method 1 says hello");
lock.wait();
System.out.println("Back again in method one");
}
}
private static void two() throws InterruptedException{
synchronized (lock){
System.out.println("Method 2 says hello");
lock.notify();
System.out.println("Hello from method two even after notifying");
}
}
}