Home >Java >javaTutorial >Overview of Lock API in java
Managing access to shared resources is important in concurrent programming to ensure data consistency. Traditional synchronized keyword lacks flexibility for complex scenarios like fairness, acquire lock immediately, wait for a specific time to acquire lock, etc. Lock, ReadWriteLock, and StampedLock APIs are introduced in Java 1.5 to addresses these limitation and provides great control to developers to handle shared resources access. They are part of java.util.concurrent package.
Lock API is an interface and provides below methods to handle thread synchronization.
ReentrantLock
ReentrantLock lock = new ReentrantLock(true); //fair lock public void methodExecution() { lock.lock(); try { // Critical section here } finally { lock.unlock(); } }
ReadWriteLock API is an interface and maintains a pair of locks to maintain read and write scenarios. The read lock can be held simultaneously by multiple threads if there are no writers. The write lock is exclusive.
Key Rules
Example: In micro-service communication, assume Service B expects a JWT from Service A. JWT is generated by Service A and can be cacheable for couple of minutes. In this scenarios ReentrantReadWriteLock will be more useful. We can renew the token if it is expired or about to expire. I am not acquiring read lock here to avoid read starvation.
ReentrantLock lock = new ReentrantLock(true); //fair lock public void methodExecution() { lock.lock(); try { // Critical section here } finally { lock.unlock(); } }
StampedLock was introduced in Java 8 and it is a class. It supports three modes of locking and returns a stamp that is used to release the lock. It allows lock upgradation.
Key Points:
String jwt = JwtUtil.generateJwt(); ReadWriteLock lock = new ReentrantReadWriteLock(); Lock writeLock = lock.writeLock(); Lock readLock = lock.readLock(); public String getJwt(String key, String value) { if (StringUtils.hasLength(jwt)) { if (JwtUtil.isJwtEligibleToRenew(jwt)) { generateJwt(); } } else { generateJwt(); } return this.jwt; } public void generateJwt() { this.writeLock.lock(); //write lock try { if (JwtUtil.isJwtEligibleToRenew(jwt)) { this.jwt = JwtUtil.generateJwt(); } } finally { this.writeLock.unlock(); //release write lock } }
Optimistic read example. This allows a thread to read without acquiring a traditional read lock, which improves performance as it avoids lock contention. If any write lock acquired after acquiring optimistic read lock time, validate() will return false otherwise it will be true.
public void readAndWrite() { long l = this.stampedLock.readLock(); try { //critical section } finally { this.stampedLock.unlock(l); } long w = this.stampedLock.writeLock(); try { //critical section } finally { this.stampedLock.unlock(w); } }
Happy Coding and Learning !!!
Please drop a comment if you have any question.
The above is the detailed content of Overview of Lock API in java. For more information, please follow other related articles on the PHP Chinese website!