-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiThreadedProgram.java
More file actions
56 lines (43 loc) · 1.07 KB
/
MultiThreadedProgram.java
File metadata and controls
56 lines (43 loc) · 1.07 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
53
54
55
56
import java.util.Random;
class NumberGenerator extends Thread {
public void run() {
Random rand = new Random();
while (true) {
int num = rand.nextInt(100);
System.out.println("Generated Number: " + num);
if (num % 2 == 0) {
new Square(num).start();
} else {
new Cube(num).start();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Square extends Thread {
int num;
Square(int num) {
this.num = num;
}
public void run() {
System.out.println("Square of " + num + " = " + (num * num));
}
}
class Cube extends Thread {
int num;
Cube(int num) {
this.num = num;
}
public void run() {
System.out.println("Cube of " + num + " = " + (num * num * num));
}
}
public class MultiThreadedProgram {
public static void main(String[] args) {
new NumberGenerator().start();
}
}