Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockFairnessDemo {
private static ReentrantLock reentrantLock = new ReentrantLock(true);
public static void main(String[] args) throws InterruptedException {
ReentrantLockFairnessDemo reentrantLockDemo = new ReentrantLockFairnessDemo();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
reentrantLockDemo.checkSync();
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
reentrantLockDemo.checkSync();
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
reentrantLockDemo.checkSync();
}
});
t1.start();
Thread.sleep(1000);
t2.start();
Thread.sleep(1000);
t3.start();
t1.join();
t2.join();
t3.join();
System.out.println("Finished..");
}
private void checkSync() {
reentrantLock.lock();
System.out.println("Acquired.." + reentrantLock.toString());
try {
Thread.sleep(5000);
System.out.println("Done..");
} catch (InterruptedException e) {
e.printStackTrace();
}
reentrantLock.unlock();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment