解决方案:
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(); } } } } }
死锁程序:
package Demos; public class Demo_06 { public static void main(String[] args) { new DeadLock(false).start(); new DeadLock(true).start(); } } class DeadLock extends Thread{ private boolean flag=false; DeadLock(boolean flag){ this.flag=flag; } @Override public void run() { if(flag){ synchronized(Lock.lockA){ System.out.println("if lockA"); synchronized(Lock.lockB){ System.out.println("if lockB"); } } }else{ synchronized(Lock.lockB){ System.out.println("else lockA"); synchronized(Lock.lockA){ System.out.println("else lockB"); } } } } } class Lock{ static Object lockA=new Object(); static Object lockB=new Object(); }
原文地址:http://blog.csdn.net/duoduoluojia/article/details/46574013