How to do it...

Follow these steps to implement the example:

  1. Create a class named MyLock that extends the ReentrantLock class:
        public class MyLock extends ReentrantLock {
  1. Implement getOwnerName(). This method returns the name of the thread that has control of a lock (if any), using the protected method of the Lock class called getOwner():
        public String getOwnerName() {          if (this.getOwner()==null) {            return "None";          }          return this.getOwner().getName();        }
  1. Implement getThreads(). This method returns a list of threads queued in a lock, using the protected method of the Lock class called getQueuedThreads():
        public Collection<Thread> getThreads() {           return this.getQueuedThreads();         }
  1. Create a class named Task that implements the Runnable ...

Get Java 9 Concurrency Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.