-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprog5_1.java
71 lines (67 loc) · 1.56 KB
/
prog5_1.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
import java.util.Random;
class NumGen implements Runnable{
Random random=new Random();
int num;
public void run() {
while(true){
num=random.nextInt(100);
System.out.println("Generated Number: " + num);
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
}
}
public int getnum(){
return num;
}
}
class sqgen implements Runnable{
NumGen numgen;
sqgen(NumGen numgen){
this.numgen=numgen;
}
public void run(){
while(true){
int num=numgen.getnum();
int square=num*num*num;
System.out.println("Square: " + square);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class cubgen implements Runnable{
NumGen numgen;
cubgen(NumGen numgen){
this.numgen=numgen;
}
public void run(){
while(true){
int num=numgen.getnum();
int cube=num*num*num;
System.out.println("Cube"+cube);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class prog5_1{
public static void main(String[] args) {
NumGen numgen=new NumGen();
Thread thread1=new Thread(numgen);
Thread thread2=new Thread(new sqgen(numgen));
Thread thread3=new Thread(new cubgen(numgen));
thread1.start();
thread2.start();
thread3.start();
}
}