-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTwoThreads.java
More file actions
49 lines (40 loc) · 1.09 KB
/
TwoThreads.java
File metadata and controls
49 lines (40 loc) · 1.09 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
public class TwoThreads {
private static Object resource = new Object();
private static void delay(long n) {
try {
Thread.sleep(n);
}
catch(Exception e) {
System.out.println("Error:"+e.getMessage());
}
}
public static void main(String[] args) {
System.out.println("StartMain");
new Thread1().start();
delay(1000);
Thread2 t2 = new Thread2();
t2.start();
delay(1000);
t2.interrupt();
delay(1000);
System.out.println("EndMain");
}
static class Thread1 extends Thread {
public void run() {
synchronized(resource) {
System.out.println("Start 1");
delay(6000);
System.out.println("End 1");
}
}
}
static class Thread2 extends Thread {
public void run() {
synchronized(resource) {
System.out.println("Start 2");
delay(2000);
System.out.println("End 2");
}
}
}
}