forked from Shreerang4/learning-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultithread.java
More file actions
45 lines (40 loc) · 1.08 KB
/
Multithread.java
File metadata and controls
45 lines (40 loc) · 1.08 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
import java.util.Random;
class numGenThread extends Thread{
public static int n;
public void run(){
Random random = new Random();
n = random.nextInt(100);
System.out.println("\nNumGenThread: " + n);
}
}
class numSquareThread extends numGenThread{
@Override
public void run(){
System.out.println("NumSquareThread: " + n*n);
}
}
class numCubeThread extends numGenThread{
@Override
public void run(){
System.out.println("NumCubeThread: " + n*n*n);
}
}
public class Multithread {
public static void main(String[] args) {
numGenThread t1 = new numGenThread();
numGenThread t2 = new numSquareThread();
numGenThread t3 = new numCubeThread();
for(int i = 0; i<10; i++){
t1.run();
t2.run();
t3.run();
try{
t1.sleep(1000);
}
catch(InterruptedException e){
System.out.println(e.getStackTrace());
}
}
System.out.println("End of Program");
}
}