-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassMonitor.java
More file actions
47 lines (41 loc) · 1.43 KB
/
ClassMonitor.java
File metadata and controls
47 lines (41 loc) · 1.43 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
package chapter01;
import java.util.concurrent.TimeUnit;
/**
* Created by zhangzhao on 2018/8/23.
*/
// 使用synchronized关键字同步类的不同的实例方法,争抢的是同一个monitor lock
// this monitor
public class ClassMonitor {
public static synchronized void method1(){
System.out.println(Thread.currentThread().getName() + " enter to method1");
try {
TimeUnit.MINUTES.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static synchronized void method2(){
System.out.println(Thread.currentThread().getName() + " enter to method2");
try {
TimeUnit.MINUTES.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void method3(){
synchronized(this) { // 此时 和 在方法前加synchronized 效果一样
System.out.println(Thread.currentThread().getName() + " enter to method3");
try {
TimeUnit.MINUTES.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
ClassMonitor thisMonitor = new ClassMonitor();
new Thread(ClassMonitor::method1,"T1").start();
new Thread(ClassMonitor::method2,"T2").start();
new Thread(thisMonitor::method3,"T3").start();
}
}