Skip to content

Instantly share code, notes, and snippets.

@akshanshjain95
Created June 28, 2019 18:11
Show Gist options
  • Save akshanshjain95/feb46773adc354f8f1a4f38c1e0872eb to your computer and use it in GitHub Desktop.
Save akshanshjain95/feb46773adc354f8f1a4f38c1e0872eb to your computer and use it in GitHub Desktop.
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