forked from atyant-yadav/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multithread.java
41 lines (37 loc) · 920 Bytes
/
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
class ThreadTest extends Thread {
Thread t;
String name;
ThreadTest(String thread_name) {
name = thread_name;
t = new Thread(this, name);
System.out.println("Child Thread : " + this);
t.start();
}
public void run() {
try {
for (int i = 5; i >= 1; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child Thread Interrupted " + e);
}
System.out.println("Child Thread Executed");
}
}
public class multithread {
public static void main(String[] args) {
new ThreadTest("thread 1");
new ThreadTest("thread 2");
new ThreadTest("thread 3");
try {
for (int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main Thread Interrupted " + e);
}
System.out.println("Main Thread Executed");
}
}