线程安全的单例懒汉式
package com.day0206_2; /** * 使用同步机制将单例模式中的懒汉式改写成线程安全的 * */ public class BankTest { } class Bank{ private Bank(){ } private static Bank instance =null; public static Bank getInstance(){//synchronized //方式一:效率稍差 // synchronized(Bank.class) { // if (instance == null) { // instance = new Bank(); // } // // return instance; // } //方式二:效率更高 if (instance==null) { synchronized (Bank.class) { if (instance == null) { instance = new Bank(); } } } return instance; } }