---
title: "支持并发的单例模式"
author: "coolwolf911"
date: "2010-05-20T00:49:28Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-05-20T00:49:28Z"
id: 670325
url: "https://coolwolf911.iteye.com/blog/670325"
---

DCL例子

```java


public static Singleton getInstance()
{
  if (instance == null)
  {
    synchronized(Singleton.class) {  //1
      if (instance == null)          //2
        instance = new Singleton();  //3
    }
  }
  return instance;
}


```

Initialize-On-Demand 

```java


public class Foo {
    // 似有静态内部类, 只有当有引用时, 该类才会被装载
    private static class LazyFoo {
       public static Foo foo = new Foo();
    }
 
    public static Foo getInstance() {
       return LazyFoo.foo;
    }
}

```