各位大虾:对于什么时候用到同步一个类的构造函数.请指教了.谢了!!!

解决方案 »

  1.   

    单例模式。估计这个最有名。
    就是只允许有这么一个类的实例存在。不过还要配合其他方式。比如private,static之类的,具体参考单例模式。
      

  2.   

    singleton。呵呵,忘记说英文名字了。
      

  3.   

    再给个例子public class LazySingleton {    private static LazySingleton lazySingleton = null;
        private int i;
        
        private LazySingleton() {
        }    synchronized public static LazySingleton getInstance() {
            if (lazySingleton == null) {
                lazySingleton = new LazySingleton();
            }
            return lazySingleton;
        }    public void print() {
            
            System.out.println("lazySingleton:"+i++);
        }
    }