-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadLifecycleExample.java
More file actions
41 lines (31 loc) · 985 Bytes
/
Copy pathThreadLifecycleExample.java
File metadata and controls
41 lines (31 loc) · 985 Bytes
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
/**
* Day 24 - Multithreading: Thread Methods and Lifecycle
*/
public class ThreadLifecycleExample {
static class Counter extends Thread {
private String name;
Counter(String name) {
this.name = name;
}
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println(name + ": " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
}
}
}
public static void main(String[] args) throws InterruptedException {
System.out.println("=== Thread Lifecycle ===\n");
Counter thread1 = new Counter("Thread-1");
Counter thread2 = new Counter("Thread-2");
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println("All threads completed");
}
}