forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT8.java
More file actions
45 lines (36 loc) · 894 Bytes
/
T8.java
File metadata and controls
45 lines (36 loc) · 894 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
38
39
40
41
42
43
44
45
package com.basic;
import java.util.concurrent.TimeUnit;
/**
* @program JavaBooks
* @description: 锁粒度
* @author: mf
* @create: 2019/12/29 00:23
*/
public class T8 {
private int count = 0;
synchronized void m1() {
// 锁粒度较粗,锁的东西较多
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
void m2() {
// 锁粒度较细, 锁的东西较少, 运行时间比上面快
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (this) {
count++; //
}
}
}