-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate 2 threads
More file actions
52 lines (45 loc) · 1.26 KB
/
Copy pathCreate 2 threads
File metadata and controls
52 lines (45 loc) · 1.26 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
52
import java.io.*;
import java.net.*;
import java.util.Scanner;
class PrintOddEven {
private int count = 1;
private static final int MAX = 10;
public synchronized void printOdd() {
while (count < MAX) {
while (count % 2 == 0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(count + " ");
count++;
notify();
}
}
public synchronized void printEven() {
while (count <= MAX) {
while (count % 2 == 1) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(count + " ");
count++;
notify();
}
}
}
<---------Main class-------------------->
public class ThreadOddEvenTest {
public static void main(String[] args) {
PrintOddEven printer = new PrintOddEven();
Thread threadA = new Thread(printer::printOdd);
Thread threadB = new Thread(printer::printEven);
threadA.start();
threadB.start();
}
}