mercredi 5 avril 2017

Double-checked locking

In my attempt to go fast, I started reading Mastering Concurrency Programming with Java 8. I am trying to understand the Double-Checked Locking pattern but mostly I have no clue. The book offers this example as the best possible solution for a Singleton. Why is this great and how will this produce more reliable code? If you could break this down in the simplest way possible, you would be a personal hero.

public class Singleton {
  private static class LazySingleton {
    private static final Singleton INSTANCE = new Singleton();
  }
  public static Singleton getSingleton() {
    return LazySingleton.INSTANCE;
  }
}

^ Excerpt From: Javier Fernandez Gonzalez. Mastering Concurrency Programming with Java 8.

P.S. Likewise, I am wondering if anyone can tell me why Singletons are so popular in concurrent programming. The book hasn't mentioned them in detail yet but from other experiences and languages I know they a major object use in concurrency.

Is it just a simple way to cross reference state among threads and processes?

Aucun commentaire:

Enregistrer un commentaire