-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathmultithread.java
73 lines (56 loc) · 1.31 KB
/
multithread.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
import java.util.*;
class Square extends Thread {
int n;
Square(int n) {
this.n = n;
}
public void run() {
int s = this.n * this.n;
System.out.println("Square of " + this.n + " = " + s );
}
}
class Cube extends Thread {
int n;
Cube(int n) {
this.n = n;
}
public void run() {
int c = this.n * this.n * this.n;
System.out.println("Cube of " + this.n + " = " + c );
}
}
class Number extends Thread {
public void run() {
Scanner sc = new Scanner(System.in);
System.out.print("How Many Random Integers needed : ");
int x = sc.nextInt();
Random random = new Random();
System.out.print("Set the limit upto which random numbers should be generated : ");
int l = sc.nextInt();
sc.close();
for(int i =0; i<x; i++) {
int ranInt = random.nextInt(l);
System.out.println("The random integer: " + ranInt);
if(ranInt%2==0) {
Square s = new Square(ranInt);
s.start();
}
else {
Cube c = new Cube(ranInt);
c.start();
}
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
public class multithread {
public static void main(String args[]) throws Exception {
Number n = new Number();
n.start();
}
}