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 DBConnectionOK:
        public class DBConnectionOK {
  1. Declare a private constructor. Write the name of the thread that executes it:
        private DBConnectionOK() {           System.out.printf("%s: Connection created.\n",                            Thread.currentThread().getName());         }
  1. Declare a private static class named LazyDBConnectionOK. It has a private static final DBConnectionOK instance named INSTANCE:
        private static class LazyDBConnection {           private static final DBConnectionOK INSTANCE = new                                                   DBConnectionOK();         }
  1. Implement the getConnection() method. It doesn't receive any parameter and returns a DBConnectionOK object. It returns the INSTANCE object:
 public static DBConnectionOK getConnection() ...

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