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 Task1 and specify that it implements the Runnable interface:
        public class Task1 implements Runnable {
  1. Declare two private Lock attributes, named lock1 and lock2:
        private Lock lock1, lock2;
  1. Implement the constructor of the class to initialize its attributes:
        public Task1 (Lock lock1, Lock lock2) {           this.lock1=lock1;           this.lock2=lock2;         }
  1. Implement the run() method. First, get control of the lock1 object using the lock() method and write aa message in the console indicating that you have got it:
        @Override         public void run() {           lock1.lock();           System.out.printf("Task 1: Lock 1 locked\n");
  1. Then, get control of lock2 using the lock() method and write ...

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