Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Singletons

Name: Anonymous 2009-01-09 5:18

A toast for one of my favorite ways to create and interact with things. I love you, singleton. And there's only one of you.

Name: Anonymous 2009-01-10 14:38

>>21
BEWARE /prog/ THIS IS NOT THREAD SAFE, DO NOT INCLUDE THIS IN ANY ENTERPRISE APPLICATIONS.

Fixed:
public class EnterpriseSingleton {
  private static EnterpriseSingleton enterpriseSingleton;

  private EnterpriseSingleton() {
  }

  public static EnterpriseSingleton getInstance() {
    if (enterpriseSingleton == null) {
      synchronized (EnterpriseSingleton.class) {
        if (enterpriseSingleton == null) {
          enterpriseSingleton = new EnterpriseSingleton();
        }
      }
    }
    return enterpriseSingleton;
  }
}

This version also alleviates the performance overhead associated with using synchronized blocks.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List