-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiple_thread.java
More file actions
63 lines (53 loc) · 1.52 KB
/
multiple_thread.java
File metadata and controls
63 lines (53 loc) · 1.52 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
57
58
59
60
61
62
63
/*
* Write a java program that creates multiple threads. Main thread generates a
* random integer. First child thread computes the square of next three the
* numbers and there should be idle time of one second between displaying of
* each computed number. Second child thread computes the cube of next three the
* numbers and there should be idle time of two seconds between displaying of
* each computed number.
*/
package Multiple_Thread;
import java.util.Random;
class T1 extends Thread {
int val;
T1(int n) {
val = n;
}
public void run() {
try {
for (int i = 1; i <= 3; i++) {
System.out.println((val + i) + ": " + Math.pow((val + i), 2));
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class T2 extends Thread {
int val;
T2(int n) {
val = n;
}
public void run() {
try {
for (int i = 1; i <= 3; i++) {
System.out.println((val + i) + ": " + Math.pow((val + i), 3));
Thread.sleep(2000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class multiple_thread {
public static void main(String[] args){
Random rand = new Random();
int x = rand.nextInt(11);
System.out.println(x + "\n");
T1 thread1 = new T1(x);
thread1.start();
T2 thread2 = new T2(x);
thread2.start();
}
}