-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadDemo51.java
More file actions
55 lines (48 loc) · 1.12 KB
/
Copy pathThreadDemo51.java
File metadata and controls
55 lines (48 loc) · 1.12 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
53
54
55
package secondPart;
/**
* @author yangxp
* @date 2017年8月3日 下午6:19:09
* <p>
* 当存在父子类继承关系时,子类是完全可以通过“可重入锁”调用父类的同步方法
* <p>
* 出现异常就会放弃锁
*/
public class ThreadDemo51 {
public static void main(String[] args) {
ThreadDemo51Test threadDemo51Test = new ThreadDemo51Test();
threadDemo51Test.start();
}
}
class ThreadDemo51Test extends Thread {
@Override
public void run() {
Sub sub = new Sub();
sub.operateIMainMethod();
}
}
class Main {
public int i = 10;
public synchronized void operateIMainMethod() {
try {
i--;
System.out.println("main print i = " + i);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Sub extends Main {
synchronized public void operateIMainMethod() {
try {
while (i > 0) {
i--;
System.out.println("Sub print i = " + i);
Thread.sleep(1000);
super.operateIMainMethod();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}