線程安全的單例模式-懶漢式java
/* * 使用同步機制將單例模式中的懶漢式改成線程安全的 * * */ public class BankTest { } class Bank{ private Bank(){} private static Bank instance=null; public static synchronized Bank getInstance(){ /*方式一:效率稍差 */ /*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; } }