-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathFairLock.java
More file actions
27 lines (23 loc) · 729 Bytes
/
Copy pathFairLock.java
File metadata and controls
27 lines (23 loc) · 729 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
package ch3.s1;
import java.util.concurrent.locks.ReentrantLock;
public class FairLock implements Runnable {
public static ReentrantLock fairLock = new ReentrantLock(true);
// public static ReentrantLock fairLock = new ReentrantLock();
public void run() {
while (true){
try {
fairLock.lock();
System.out.println(Thread.currentThread().getName());
}finally {
fairLock.unlock();
}
}
}
public static void main(String [] args){
FairLock r1 = new FairLock();
Thread t1 = new Thread(r1,"Thread_t1");
Thread t2 = new Thread(r1,"Thread_t2");
t1.start();
t2.start();
}
}