首页 > 代码库 > 产生死锁代码 - Java
产生死锁代码 - Java
public static void main(String[] args) { final LeftRightDeadLock lock = new LeftRightDeadLock(); new Thread(){ @Override public void run() { lock.leftRight(); } }.start(); new Thread(){ @Override public void run() { lock.rightLeft(); } }.start(); } } class LeftRightDeadLock { private final Object left = new Object(); private final Object right = new Object(); public void leftRight() { synchronized (left) { System.out.println("leftRight got left lock"); try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized (right) { System.out.println("leftRight got right lock"); } } } public void rightLeft() { synchronized (right) { System.out.println("rightLeft got right lock"); synchronized (left) { System.out.println("rightLeft got left lock"); } } }
产生死锁代码 - Java
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。