-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathThisMonitor.java
More file actions
37 lines (31 loc) · 949 Bytes
/
Copy pathThisMonitor.java
File metadata and controls
37 lines (31 loc) · 949 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
33
34
35
36
37
package concurrent.cpt04;
import java.util.concurrent.TimeUnit;
import static java.lang.Thread.currentThread;
/**
* @author: mayuan
* @desc:
* @date: 2018/07/02
*/
public class ThisMonitor {
public static void main(String[] args) {
ThisMonitor thisMonitor = new ThisMonitor();
new Thread(thisMonitor::method1, "T1").start();
new Thread(thisMonitor::method2, "T2").start();
}
public synchronized void method1() {
System.out.println(currentThread().getName() + " enter to method1.");
try {
TimeUnit.MINUTES.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void method2() {
System.out.println(currentThread().getName() + " enter to method2.");
try {
TimeUnit.MINUTES.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}