关于实现线程死锁的一个例子
关于实现线程死锁的一个例子
线程对象class DeadLock
{ //模拟两个对象 private final Object obj1 = new Object(); private final Object obj2 = new Object(); public static void main(String[] args){ DeadLock dl = new DeadLock(); dl.testDeadLock(); //System.out.println("Hello World!"); } public void testDeadLock(){ new Thread(new Runnable(){ public void run(){ testSync(obj1,obj2); System.out.println("obj1..."); } }).start(); new Thread(new Runnable(){ public void run(){ testSync(obj2,obj1); System.out.println("obj2..."); } }).start(); } public static void testSync(Object obj,Object obje){ synchronized(obj){ try{ Thread.sleep(2*1000); }catch(Exception e){ e.printStackTrace(); } } synchronized(obje){ try{ Thread.sleep(2*1000); }catch(Exception e){ e.printStackTrace(); } } } }
侵删 联系614506425@qq.com