Created
June 28, 2019 17:42
-
-
Save akshanshjain95/c838d51cb5c321aab9b50f88c5ef61ef 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 ReentrantLockDemo { | |
private static ReentrantLock reentrantLock = new ReentrantLock(); | |
public static void main(String[] args) throws InterruptedException { | |
ReentrantLockDemo reentrantLockDemo = new ReentrantLockDemo(); | |
Thread t1 = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
reentrantLockDemo.checkSync(); | |
} | |
}); | |
Thread t2 = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
reentrantLockDemo.checkSync(); | |
} | |
}); | |
t1.start(); | |
t2.start(); | |
t1.join(); | |
t2.join(); | |
System.out.println("Finished.."); | |
} | |
private void checkSync() { | |
reentrantLock.lock(); | |
System.out.println("Acquired.." + reentrantLock.toString()); | |
try { | |
Thread.sleep(1000); | |
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