forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT4.java
More file actions
32 lines (27 loc) · 743 Bytes
/
T4.java
File metadata and controls
32 lines (27 loc) · 743 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
package com.basic;
import java.util.concurrent.TimeUnit;
/**
* @program JavaBooks
* @description: 一个同步方法可以调用另外一个同步方法,synchronized是可重入锁 子类同步也可以调用父类的同步方法
* @author: mf
* @create: 2019/12/26 22:00
*/
public class T4 {
synchronized void m1() {
System.out.println("m1 start");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
m2();
}
synchronized void m2() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("m2");
}
}