//多线程下的单例设计模式 class Sing { //饿汉式不存在安全问题,因为其不是线程同步的 private static Sing s = new Sing(); private Sing(){} public static Sing getInstance() { return s; } } class Single { private static Single s = null; private Single(){} public static Single getInstance() { if(s==null) { synchronized(Single.class) { if(s==null) s = new Single(); } } return s; } /*public static xynchronized Single getInstance() * { * if(s==null) * s = new Single() * * return s; * } * **/ }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-09-30 16:10:51