-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadMethods.java
More file actions
46 lines (42 loc) · 1.06 KB
/
ThreadMethods.java
File metadata and controls
46 lines (42 loc) · 1.06 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
class MyThr1 extends Thread {
@Override
public void run() {
int i = 0;
while (true) {
System.out.println(i + ". Thread 1");
// Sleep for 455 ms
try {
Thread.sleep(455); // ? .Thread.sleep()
if (i == 10) {
throw new InterruptedException();
}
} catch (InterruptedException e) {
System.out.println(e);
break;
}
i++;
}
}
}
class MyThr2 extends Thread {
@Override
public void run() {
while (true) {
System.out.println("Thread 2");
}
}
}
public class ThreadMethods {
public static void main(String[] args) {
MyThr1 t1 = new MyThr1();
MyThr2 t2 = new MyThr2();
t1.start();
try {
t1.join(); // ? .join()
// t1 will finish before t2 starts
} catch (Exception e) {
System.out.println(e);
}
t2.start(); // t2 will start after t1 finishes
}
}