Lock in JavaIn Java, Lock is an interface available in the Java.util.concurrent.locks package. Java lock acts as thread synchronization mechanisms that are similar to the synchronized blocks. After some time, a new locking mechanism was introduced. It is very flexible and provides more options in comparison to the Synchronized block. These are some of the following differences between a Synchronized block and a Lock:
In Java, the Lock interface basically provides six methods which are as follows: The lock() methodThe lock() method is one of the most important methods of the Lock interface. It is used for acquiring the lock. For thread scheduling purposes, the current thread becomes disabled when the lock is not available. The lock() method is a public method that returns void. Syntax: Note: The lock implementation can be used for finding an incorrect use of a lock. In our code, the invocation can create a deadlock and can throw an unchecked exception.The lockInterruptibly() methodThe lockInterruptibly() method is another important method of Lock interface that is used for acquiring the lock unless the current thread is interrupted. It acquires and returns a lock immediately if it is available. Just like the lock() method, the current thread becomes disabled for thread scheduling purposes only when the lock is not available. At this time, the thread remains idle until the lock is not acquired or some other thread will not interrupt the current thread. It throws the InterruptedException when the current thread is interrupted at the time of acquiring the lock. The lockInterruptibly() method is a public method that returns void and takes no parameters: Syntax: The tryLock() methodThe tryLock() method is mainly used at the time of invocation for acquiring the lock. It returns the lock immediately with the Boolean value true when the lock is available. It returns the Boolean value false when the lock is not available. The tryLock() method take no parameters and return the Boolean value. Syntax: We use the tryLock() method in the following way: The tryLock(long time, TimeUnit unit) methodIt is another variation of the tryLock() method which is used for acquiring the lock when:
It acquires and returns a lock immediately if it is available. Just like the lock() method, the current thread becomes disabled for thread scheduling purposes only when the lock is not available. At this time, the thread remains idle until the lock is not acquired by the current thread. Some other threads will not interrupt the current thread, or the specified waiting time elapses. Syntax: Parameters: time: The time parameter defines the maximum time to wait for the lock. unit: The unit parameter defines the time unit of the time argument. Note: The tryLock(long time, TimeUnit unit) throws InterruptedExeption when the current thread is interrupted at the time of acquiring the lock.The unlock() methodThe unlock() method is another most common method which is used for releasing the lock. The unlock() method is a public method that returns nothing and takes no parameter. Syntax: The newCondition() methodThe newCondition() method is used for getting a new Condition instance that is bound to this Lock instance. The lock must be held by the current thread before waiting on the condition. A call to the condition.wait() will atomically release the lock before the wait and re-acquire the lock before the wait returns. Syntax: Note: The newCondition() throws UnsupportedOperationExeption when the lock implementation doesn't support conditions.LockExample.java Output: Next TopicSegment Tree in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India