-
Notifications
You must be signed in to change notification settings - Fork 0
/
concurrency.java
38 lines (33 loc) · 1.06 KB
/
concurrency.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
class MyThread extends Thread {
public MyThread(String name) {
super(name);
start();
}
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Thread " + getName() + ": Count - " + i);
try {
sleep(500);
} catch (InterruptedException e) {
System.out.println("Thread " + getName() + " interrupted.");
}
}
System.out.println("Thread " + getName() + " exiting.");
}
}
public class MyThreadDemo {
public static void main(String[] args) {
System.out.println("Main thread starting.");
MyThread childThread = new MyThread("Child Thread");
for (int i = 1; i <= 5; i++) {
System.out.println("Main Thread: Count - " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
}
System.out.println("Main thread ending.");
}
}