How it works...

In this recipe, you implemented the MyLock class that extends the ReentrantLock class to return information that wouldn't have been available otherwise-it's protected data of the ReentrantLock class. The methods implemented by the MyLock class are as follows:

  • getOwnerName(): Only one thread can execute a critical section protected by a Lock object. The lock stores the thread that is executing the critical section. This thread is returned by the protected getOwner() method of the ReentrantLock class.
  • getThreads(): When a thread is executing a critical section, other threads that try to enter it are put to sleep before they continue executing that critical section. The protected method getQueuedThreads() of the ReentrantLock class returns the list of threads that are waiting to execute the critical section.

We also used other methods that are implemented in the ReentrantLock class:

  • hasQueuedThreads(): This method returns a Boolean value indicating whether there are threads waiting to acquire the calling ReentrantLock
  • getQueueLength(): This method returns the number of threads that are waiting to acquire the calling ReentrantLock
  • isLocked(): This method returns a Boolean value indicating whether the calling ReentrantLock is owned by a thread
  • isFair(): This method returns a Boolean value indicating whether the calling ReentrantLock has fair mode activated
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.146.180