线程安全且高效的单例
生活随笔
收集整理的這篇文章主要介紹了
线程安全且高效的单例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public class Singleton{
private static Singleton instance;//靜態的實例
private Singleton(){}//私有的構造函數
public static Singleton getInstance(){
if(instance == null){
synchronized(Singleton.class){//鎖住整個對象
if(instance==null){//再次判斷是否為空,防止沒鎖對象時判斷完后其他線程實例化
instance = new Singleton();
}
}
}
return instance;
}
}
轉載于:https://www.cnblogs.com/zeng-wei/p/3394519.html
總結
以上是生活随笔為你收集整理的线程安全且高效的单例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jQuery 图片滚动效果
- 下一篇: Delphi从Internet下载文件