forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT9.java
More file actions
44 lines (34 loc) · 1.01 KB
/
T9.java
File metadata and controls
44 lines (34 loc) · 1.01 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
package com.basic;
import java.util.concurrent.TimeUnit;
/**
* @program JavaBooks
* @description: 锁对象变了,锁就被释放了
* @author: mf
* @create: 2019/12/29 00:28
*/
public class T9 {
Object o = new Object();
void m() {
synchronized (o) {
while (true) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()); // 打印自己的名字
}
}
}
public static void main(String[] args) {
T9 t9 = new T9();
new Thread(t9::m, "t9").start();
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
t9.o = new Object(); // 注释掉这句话的话, t9二代是不会启动的,因为还在上锁呢。
new Thread(t9::m, "t9二代").start();
}
}