Skip to content

Commit cc7bdf4

Browse files
committed
Create multiple_thread.java
1 parent fd0095f commit cc7bdf4

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Write a java program that creates multiple threads. Main thread generates a
3+
* random integer. First child thread computes the square of next three the
4+
* numbers and there should be idle time of one second between displaying of
5+
* each computed number. Second child thread computes the cube of next three the
6+
* numbers and there should be idle time of two seconds between displaying of
7+
* each computed number.
8+
*/
9+
10+
package Muttiple_Thread;
11+
12+
import java.util.Random;
13+
14+
class T1 extends Thread {
15+
int val;
16+
17+
T1(int n) {
18+
val = n;
19+
}
20+
21+
public void run() {
22+
try {
23+
for (int i = 1; i <= 3; i++) {
24+
System.out.println((val + i) + ": " + Math.pow((val + i), 2));
25+
Thread.sleep(1000);
26+
}
27+
} catch (InterruptedException e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
}
32+
33+
34+
class T2 extends Thread {
35+
int val;
36+
37+
T2(int n) {
38+
val = n;
39+
}
40+
41+
public void run() {
42+
try {
43+
for (int i = 1; i <= 3; i++) {
44+
System.out.println((val + i) + ": " + Math.pow((val + i), 3));
45+
Thread.sleep(2000);
46+
}
47+
} catch (InterruptedException e) {
48+
e.printStackTrace();
49+
}
50+
}
51+
}
52+
53+
public class multiple_thread {
54+
public static void main(String[] args){
55+
Random rand = new Random();
56+
int x = rand.nextInt(11);
57+
System.out.println(x + "\n");
58+
T1 thread1 = new T1(x);
59+
thread1.start();
60+
T2 thread2 = new T2(x);
61+
thread2.start();
62+
}
63+
}

0 commit comments

Comments
 (0)