-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReentrantLockTest.java
More file actions
41 lines (36 loc) · 981 Bytes
/
ReentrantLockTest.java
File metadata and controls
41 lines (36 loc) · 981 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
package concurrent.lock;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Created by Edwin Xu on 6/27/2020 2:06 PM
*/
public class ReentrantLockTest {
/*
* ReentrantLock 实现了Lock接口,
* 提供更加细粒度和灵活的锁操作
*
* - 手动
* - 灵活
* - 公平锁
* - 非公平锁
* -
* */
private static final Lock lock = new ReentrantLock(true);
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(()->test(),"线程"+i).start();
}
}
public static void test(){
try{
lock.lock();
System.out.println(Thread.currentThread().getName()+" is using");
}catch (Exception e){
e.printStackTrace();
}
finally {
System.out.println(Thread.currentThread().getName()+"is leaving");
lock.unlock();
}
}
}