Skip to content

Commit 317a416

Browse files
authored
Add files via upload
1 parent 496f326 commit 317a416

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

Multithreading/Multithreading5.java

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package Multithreading;
2+
class thread implements Runnable {
3+
public void run() {
4+
// moving thread2 to timed waiting state
5+
try {
6+
Thread.sleep(1500);
7+
} catch (InterruptedException e) {
8+
e.printStackTrace();
9+
}
10+
11+
System.out.println("State of thread1 while it called join() method on thread2 -" + Multithreading5.thread1.getState());
12+
try {
13+
Thread.sleep(200);
14+
} catch (InterruptedException e) {
15+
e.printStackTrace();
16+
}
17+
}
18+
}
19+
20+
public class Multithreading5 implements Runnable {
21+
public static Thread thread1;
22+
public static Multithreading5 obj;
23+
24+
public static void main(String[] args) {
25+
obj = new Multithreading5();
26+
thread1 = new Thread(obj);
27+
28+
// thread1 created and is currently in the NEW state.
29+
System.out.println("State of thread1 after creating it - " + thread1.getState());
30+
thread1.start();
31+
32+
// thread1 moved to Runnable state
33+
System.out.println("State of thread1 after calling .start() method on it - " + thread1.getState());
34+
}
35+
36+
public void run() {
37+
thread myThread = new thread();
38+
Thread thread2 = new Thread(myThread);
39+
40+
// thread1 created and is currently in the NEW state.
41+
System.out.println("State of thread2 after creating it - " + thread2.getState());
42+
thread2.start();
43+
44+
// thread2 moved to Runnable state
45+
System.out.println("State of thread2 after calling .start() method on it - " + thread2.getState());
46+
47+
// moving thread1 to timed waiting state
48+
try {
49+
// moving thread1 to timed waiting state
50+
Thread.sleep(200);
51+
} catch (InterruptedException e) {
52+
e.printStackTrace();
53+
}
54+
System.out.println("State of thread2 after calling .sleep() method on it - " + thread2.getState());
55+
56+
try {
57+
// waiting for thread2 to die
58+
thread2.join();
59+
} catch (InterruptedException e) {
60+
e.printStackTrace();
61+
}
62+
System.out.println("State of thread2 when it has finished it's execution - " + thread2.getState());
63+
}
64+
65+
}

0 commit comments

Comments
 (0)