public class DeadLockDemo { public void method1() { synchronized (String.class) { System.out.println("method1 lock1……"); synchronized (Integer.class) { System.out.println("method1 lock2……"); } } } public void method2() { synchronized (Integer.class) { System.out.println("method2 lock1……"); synchronized (String.class) { System.out.println("method2 lock2……"); } } } }
单例模式
饿汉式 class Singleton { private static Singleton instance=new Singleton(); private Singleton(){} static Singleton getInstance() { return instance; } } 懒汉式 class Singleton { private static Singleton instance=null; private Singleton(){} static Singleton getInstance() { if(instance==null) instance=new Singleton(); return instance; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)