java.util.concurrent.lock package{1.5 version}
Problem with tradition synchronized keyword:
- We are not having any flexibility to try for a lock without waiting.
- There is no way to specify the maximum waiting time for a thread to get the lock whereas in tradition synchronized thread will wait until getting the lock which may cause performance problems or deadlock.
- In tradition synchronized, if a thread releases the lock then which waiting thread will get that lock we are not having control over this
Lock Interface:
Lock object is similar to the implicit lock to execute synchronized methods or blocks. Lock interface provides more extensive operation than traditional lock(synchronized).
Important methods of Lock interface:
- void lock() - we can use this method to acquire a lock.
- if lock already available then the current thread will get that lock.
- if the lock is not available then it will wait until getting a lock it is the exact same behavior of tradition synchronized keywords.
- boolean tryLock() -To acquire the lock without waiting, if the lock is available then the thread acquires the lock. if the lock is not available then this method returns false and can continue its execution without waiting.
Syntax:
if(l.tryLock())//l means reentrant lock we will see this later
{
perform operation;
}
else{
perform alternative operation;
}
- boolean tryLock(long time, TimeUnit unit)- if the lock is available then the thread gets the lock and continues its execution and if the lock is not available then the thread will wait for a specify amount of time only.
TimeUnit:
it is enum present in java.util.concurrent package.
enum TimeUnit{
NANOSECONDS;
MICROSECONDS;
MILLISECONDS;
SECONDS;
MINUTES;
HOUR;
DAYS;
}
Syntax:
if(l.tryLock(1000,TimeUnit.MILLISECONDS))
{
perform operation;
}
else{
alternative operation;
}
- void lockInterruptibly()- it acquires the lock and if the lock is not available it will wait for the lock while waiting if the thread interrupted then thread won't get the lock.
- void unlock()- To release the lock.
ReentrantLock Class:-
it is the implementation class of the lock interface and it is the direct child class of object. Reentrant means a thread can acquire the same lock multiple times without any issue internally reentrant lock increment thread personal count whenever we call lock() and decrement whenever thread call unlock() and the lock will be released whenever lock thread personal count reaches 0.
Constructor of ReentrantLock:-
- ReentrantLock l=new ReentrantLock()- create the instance of reentrantLock(object of reentrantLock).
- ReentantLock l=new ReentrantLock(boolean fairness)- Create ReentrantLock with the given fairness policy if the fairness is true then the longest waiting thread can get the lock if it is available. If fairness policy is false then which waiting thread will get the chance we can't expect.
Methods of ReentrantLock:-
- int getHoldCount()- return number of hold on this lock by the current thread.
- boolean isHeldByCurrentThread()- return true iff lock is hold by current thread.
- int getQueueLength()- return number of thread waiting for the lock.
- collection getQueuedThreads- return a collection of thread waiting for the lock.
- boolean hasQueuedThreads()- return true if any thread waiting to get the lock.
- boolean isLocked()- return true if the lock is aquired.
- boolean isFair()- return true if the fairness policy is set to true otherwise false.
- Thread getOwner()- return the thread which acquired the lock.
Programs:
import java.util.concurrent.locks.ReentrantLock;//its mandatory to import.
class ReentrantDemo1 extends Thread{
public static void main(String args[])
{
ReentrantLock l=new ReentrantLock();
l.lock();//HoldCount set to 1
l.lock();//HoldCount incremented by 1 which is 2
System.out.println("True(lock) or false(not lock)= "+l.isLocked());
System.out.println("Is lock held by current Thread= "+l.isHeldByCurrentThread());
System.out.println("Number of threads waiting to get the lock= "+l.getQueueLength());
System.out.println("Hold count= "+l.getHoldCount());//2
System.out.println("Fairness policy= "+l.isFair());
}
}
Comments
Post a Comment