package com.lu; public class Test { private volatile static Test instance = null; public static Test getInstance() { if (null == instance) { synchronized (Test.class) { if (null == instance) { instance = new Test(); } } } return instance; } public static void main(String[] args) { new GetThread().start(); new GetThread().start(); new GetThread().start(); } } class GetThread extends Thread { @Override public void run() { System.out.println(Test.getInstance()); } }
參考書籍:《Head First 設計模式》java