Created
June 28, 2019 18:11
-
-
Save akshanshjain95/feb46773adc354f8f1a4f38c1e0872eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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