public class Singleton { private Singleton() {} //關鍵點0:構造函數是私有的 private static Singleton single = null; //關鍵點1:聲明單例對象是靜態的 private static object obj= new object(); public static Singleton GetInstance() //經過靜態方法來構造對象 { if (single == null) //關鍵點2:判斷單例對象是否已經被構造 { lock(obj) //關鍵點3:加線程鎖 { if(single == null) //關鍵點4:二次判斷單例是否已經被構造 { single = new Singleton(); } } } return single; } }