Javatpoint Logo
Javatpoint Logo

Java ReentrentLock class

The ReentrantLock class implements the Lock interface which is used to enhance the multithreading. It provides the capability to avoid the use of synchronization to maintain the database consistency so that the waiting time of the threads can be reduced. A Thread owns the ReentrantLock which is intended to perform the write operation on some particular data.

Java ReentrentLock class declaration

List of Array class Methods

NO Method Description
1. public int getHoldCount() The getHoldCount() method of ReentrantLock class Queries the number of holds on this lock by the current thread.
2. protected Thread getOwner() The getOwner() method of ReentrantLock class returns the thread that currently owns this lock, or null if not owned. When this method is called by a thread that is not the owner, the return value reflects a best-effort approximation of current lock status.
3. protected Collection getQueuedThreads() The getQueuedThreads() returns a collection containing threads that may be waiting to acquire this lock. Because the actual threads may change dynamically while constructing this result, the returned collection is only a best-effort estimate.
4. public final boolean hasQueuedThread(Thread thread) The hasQueuedThread(Thread thread) method of ReentrantLock class Queries whether the given thread is waiting to acquire this lock. Note that because cancellations may occur at any time, a true return does not guarantee that this thread will ever acquire this lock. This method is designed primarily for use in monitoring of the system state.
5. public final boolean hasQueuedThreads() The hasQueuedThreads() Queries whether any threads are waiting to acquire this lock. Note that because cancellations may occur at any time, a true return does not guarantee that any other thread will ever acquire this lock.
6. public final boolean isFair() The isFair() method of ReentrantLock class returns true if this lock has fairness set true.
7. public boolean isHeldByCurrentThread() The isHeldByCurrentThread() method of Reentrant Class checks if the current thread occupies this lock.
8. public boolean isLocked() The isLocked()method of ReentrantLock class checks if any thread occupies this lock. This method is created for use in monitoring of the system state, not for synchronization control.
9. public void lock() The lock() method of Reentrant Class hold the lock if it is not held by another thread and returns immediately, and set the lock hold count to one.
10. public boolean tryLock() The tryLock() method of ReentrantLock class holds the lock only when any other thread does not hold it at the time of invocation. If the current thread already holds this lock, then the hold count is incremented by one, and the method returns true. Otherwise false.
11. public final int getQueueLength() The getQueueLength() method of Reentrant Class returns the number of threads waiting to get the lock. The value is approximate because the number of threads can change dynamically while traversing internal data structures. It is used to monitor the state.
12. public void lockInterruptibly() throws InterruptedException The lockInterruptibly() method of ReentrantLock class hold the lock until or unless the current thread is interrupted.
13. public String toString() The setByte() method of Array class sets the value of the indexed component of the specified array object to the specified byte value.

Java ReentrentLock class Example: hasQueuedThreads()

Test it Now

Output:

Thread-1 is Waiting to get the lock
Thread-1 has got the  lock.
 has queued Threads = false
Thread-1 has released the lock.

Java ReentrentLock Class Example: lock()

Test it Now

Output:

after sleep(1500) Is locked - true
task name - Job1 work done

Java ReentrentLock Class Example: isHeldByCurrentThread()

Test it Now

Output:

    0  Thread-0
    1  Thread-0
    4  5
    5  5
 after sleep(1500) Is held by Current Thread - true
task name - Job1 work done
Next TopicJava ReentrantLock





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA