-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadDemo.java
More file actions
38 lines (36 loc) · 961 Bytes
/
Copy pathThreadDemo.java
File metadata and controls
38 lines (36 loc) · 961 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
class MyThread1 extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Thead 1");
}
}
}
class MyThread2 implements Runnable {
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("Thead 2");
}
}
}
public class ThreadDemo {
public static void main(String[] args) {
try {
Thread t1 = new Thread(new MyThread1());
Thread t2 = new Thread(new MyThread2());
t1.start();
t2.start();
for (int i = 0; i < 5; i++) {
System.out.println("Main Thread");
}
t2.join();
t1.join();
} catch (InterruptedException e) {
System.out.println(e);
}
}
}