-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadExamples.java
More file actions
46 lines (41 loc) · 1.21 KB
/
Copy pathThreadExamples.java
File metadata and controls
46 lines (41 loc) · 1.21 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
import java.io.*;
import java.util.*;
class ThreadExample implements Runnable {
String name;
Thread thread;
ThreadExample(String name) {
this.name = name;
thread = new Thread(this, name);
System.out.println("A new thread " + thread + " is created");
thread.start();
}
public void run() {
try {
for (int j = 1; j <= 5; j++) {
System.out.println(name + " : " + j);
Thread.sleep(100);
}
} catch (InterruptedException e) {
System.out.println(name + " thread is interrupted");
}
System.out.println(name + " thread exiting.");
}
}
class ThreadExmaples {
public static void main(String[] args) {
new ThreadExample("1st");
try {
Thread.sleep(600);
} catch (InterruptedException e) {
System.out.println("Bakchodi");
}
new ThreadExample("2nd");
new ThreadExample("3rd");
try {
Thread.sleep(800);
} catch (InterruptedException e) {
System.out.println("Interruption in main threaad");
}
System.out.println("Exiting from the main thread");
}
}