-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprog5.java
80 lines (66 loc) · 2.11 KB
/
prog5.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.Random;
class NumberGenerator3 implements Runnable {
private int number;
private final Random random = new Random();
public void run() {
while (true) {
number = random.nextInt(100); // Generate a random integer
System.out.println("Generated Number: " + number);
try {
Thread.sleep(1000); // Sleep for 1 second
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public int getNumber() {
return number;
}
}
class SquareCalculator3 implements Runnable {
private final NumberGenerator3 numberGenerator;
public SquareCalculator3(NumberGenerator3 numberGenerator) {
this.numberGenerator = numberGenerator;
}
public void run() {
while (true) {
int number = numberGenerator.getNumber();
int square = number * number;
System.out.println("Square: " + square);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class CubePrinter3 implements Runnable {
private final NumberGenerator3 numberGenerator;
public CubePrinter3(NumberGenerator3 numberGenerator) {
this.numberGenerator = numberGenerator;
}
public void run() {
while (true) {
int number = numberGenerator.getNumber();
int cube = number * number * number;
System.out.println("Cube: " + cube);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class prog5 {
public static void main(String[] args) {
NumberGenerator3 numberGenerator = new NumberGenerator3();
Thread thread1 = new Thread(numberGenerator);
Thread thread2 = new Thread(new SquareCalculator3(numberGenerator));
Thread thread3 = new Thread(new CubePrinter3(numberGenerator));
thread1.start();
thread2.start();
thread3.start();
}
}