-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultithreading_demo.java
More file actions
executable file
·52 lines (41 loc) · 1.22 KB
/
Copy pathmultithreading_demo.java
File metadata and controls
executable file
·52 lines (41 loc) · 1.22 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
47
48
49
50
51
52
class FirstThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("First Thread: " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
class SecondThread implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Second Thread: " + i);
try {
Thread.sleep(700);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
public class MultiThreadDemo {
public static void main(String[] args) {
FirstThread t1 = new FirstThread();
Thread t2 = new Thread(new SecondThread());
t1.start();
t2.start();
for (int i = 1; i <= 5; i++) {
System.out.println("Main Thread: " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e);
}
}
System.out.println("All threads finished!");
}
}