O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required