This repository was archived by the owner on Jul 12, 2023. It is now read-only.
forked from polyglot-compiler/JLang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotifyTest.java
57 lines (53 loc) · 1.53 KB
/
NotifyTest.java
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
public class NotifyTest {
final static Object lock = new Object();
static boolean runA = true;
static int i = 0;
static class A extends Thread {
@Override
public void run() {
for (int j = 0; j < 10; j++) {
synchronized (lock) {
while (!runA) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("A prints " + i);
i++;
runA = !runA;
lock.notify();
}
}
}
}
static class B extends Thread {
@Override
public void run() {
for (int j = 0; j < 10; j++) {
synchronized (lock) {
while (runA) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("B prints " + i);
i++;
runA = !runA;
lock.notify();
}
}
}
}
public static void main(String[] args) throws Exception {
A a = new A();
B b = new B();
a.start();
b.start();
a.join();
b.join();
}
}