第一:利用内部类加继承关系
class A{ protected class B extends A{ // public B(){System.out.println("this is B constructor");} } public B b =new B(); public A(){ //System.out.println("this is A constructor"); } } public class TestInnerClass{ public static void main(String args[]){ new A(); } }
第二: 利用normal类加继承
class A{ public TestDeadlock t = new TestDeadlock(); } public class TestDeadlock extends A{ public static void main(String args[]){ new A(); } }
或者:
class A{ public A(){ TestDeadlock t = new TestDeadlock(); } } public class TestDeadlock extends A{ public static void main(String args[]){ new A(); } }
第三 可以不用构造函数 和 继承也可以实现
class Blocked{ public Interrupt interrupt = new Interrupt(); } public class Interrupt{ Blocked b = new Blocked(); public static void main(String[] args) { new Blocked(); } }
总结: 其实就是利用继承关系让父子类发生死锁。
在父类中实例化子类是产生死锁的根本原因。
其实就是资源相互竞争,形成死循环