Skip to content

Commit 9e438cb

Browse files
authored
Add files via upload
1 parent a52e553 commit 9e438cb

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

Multithreading/Multithreading4.java

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package Multithreading;
2+
3+
import java.util.Random;
4+
5+
/**
6+
* Multithreading4
7+
*/
8+
class PrintTask extends Thread {
9+
10+
11+
public void run()
12+
{
13+
Random generator=new Random();
14+
15+
int sleeptime=generator.nextInt(5000);
16+
17+
try {
18+
System.out.printf("%s is going to sleep for %d milliseconds\n",Thread.currentThread().getName(),sleeptime);
19+
Thread.sleep(sleeptime);
20+
21+
} catch (InterruptedException e) {
22+
e.printStackTrace();
23+
}
24+
System.out.println(Thread.currentThread().getName()+" state after .sleep is called:"+Thread.currentThread().getState());
25+
26+
System.out.println(Thread.currentThread().getName()+" done sleeping");
27+
}
28+
}
29+
/**
30+
* Multithreading4
31+
*/
32+
public class Multithreading4 {
33+
34+
public static void main(String[] args) {
35+
PrintTask t1=new PrintTask();
36+
System.out.println("State of "+t1.getName()+" after creating it:"+t1.getState());
37+
38+
PrintTask t2=new PrintTask();
39+
System.out.println("State of "+t2.getName()+" after creating it:"+t2.getState());
40+
41+
PrintTask t3=new PrintTask();
42+
System.out.println("State of "+t3.getName()+" after creating it:"+t3.getState());
43+
44+
t1.start();
45+
System.out.println("State of " + t1.getName() + " after .start is called :" + t1.getState());
46+
try {
47+
System.out.println("Current Thread: " + Thread.currentThread().getName());
48+
t1.join();
49+
System.out.println(t1.getName()+" After .join is called "+t1.getState());
50+
}
51+
52+
catch (Exception ex) {
53+
System.out.println("Exception has " + "been caught" + ex);
54+
}
55+
t2.start();
56+
System.out.println("State of " + t2.getName() + " after .start is called :" + t2.getState());
57+
try {
58+
System.out.println("Current Thread: " + Thread.currentThread().getName());
59+
t2.join();
60+
System.out.println(t2.getName() + " After .join is called " + t2.getState());
61+
}
62+
63+
catch (Exception ex) {
64+
System.out.println("Exception has " + "been caught" + ex);
65+
}
66+
t3.start();
67+
System.out.println("State of " + t3.getName() + " after .start is called :" + t3.getState());
68+
69+
}
70+
}

0 commit comments

Comments
 (0)