forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT3.java
More file actions
39 lines (34 loc) · 1 KB
/
T3.java
File metadata and controls
39 lines (34 loc) · 1 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
package com.basic; /**
* @program JavaBooks
* @description: 同步和非同步方法是否可以同时使用
* @author: mf
* @create: 2019/12/26 21:30
*/
/**
* m1方法执行过程当中,能否执行m2
* 可以执行
*/
public class T3 {
public synchronized void m1() { // 锁定的对象的同步方法
System.out.println(Thread.currentThread().getName() + " m1 start ...");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " m1 end ...");
}
public void m2() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " m2 ");
}
public static void main(String[] args) {
T3 t3 = new T3();
new Thread(() -> t3.m1(), "t1").start();
new Thread(() -> t3.m2(), "t2").start();
}
}