Synchronized Singleton : Singleton « Design Pattern « Java Tutorial






public class TestSingletonSynchronized implements java.lang.Runnable {
  Thread thread;

  public static void main(String args[]) {
    TestSingletonSynchronized t = new TestSingletonSynchronized();
  }

  public TestSingletonSynchronized() {
    SynchronizedData database = SynchronizedData.getInstance("products");

    thread = new Thread(this, "second");
    thread.start();

    System.out.println("This is the " + database.getName() + " database.");
  }

  public void run() {
    SynchronizedData database = SynchronizedData
        .getInstance("employees");

    System.out.println("This is the " + database.getName() + " database.");
  }
}

class SynchronizedData {
  private static SynchronizedData singleObject;
  private int record;
  private String name;

  private SynchronizedData(String n) {
    name = n;
    record = 0;
  }

  public static synchronized SynchronizedData getInstance(String n) {
    if (singleObject == null) {
      singleObject = new SynchronizedData(n);
    }

    return singleObject;
  }

  public void edit(String operation) {
    System.out.println("Performing a " + operation + " operation on record "
        + record + " in database " + name);
  }

  public String getName() {
    return name;
  }
}








34.1.Singleton
34.1.1.Basic Singleton
34.1.2.The Singleton design pattern: you can never instantiate more than one.
34.1.3.Singleton test
34.1.4.Synchronized Singleton
34.1.5.Singleton Factory