forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT14.java
More file actions
37 lines (28 loc) · 812 Bytes
/
T14.java
File metadata and controls
37 lines (28 loc) · 812 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 com.basic;
import java.util.concurrent.locks.ReentrantLock;
/**
* @program JavaBooks
* @description: ReentrantLock的公平锁
* @author: mf
* @create: 2019/12/30 20:42
*/
public class T14 {
private static ReentrantLock lock = new ReentrantLock(true); // true为公平锁
public void m() {
for (int i = 0; i < 1000; i++) {
lock.lock();
try {
System.out.println(Thread.currentThread().getName() + "获得锁");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
public static void main(String[] args) {
T14 t14 = new T14();
new Thread(t14::m, "t1").start();
new Thread(t14::m, "t2").start();
}
}