Thread DeadLock in java
SOURCE : Deadlock.javapublic class Deadlock { public static void main(String[] args) { final String r1 = "resource1"; final String r2 = "resource2"; Thread t1 = new Thread() { public void run() { synchronized (r1) { System.out.println(" i m in resource 1 , waiting for 2 "); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized (r2) { System.out.println(" i m in resource 2 "); } } } }; Thread t2 = new Thread() { public void run() { synchronized (r1) { System.out.println(" i m in resource 2 , waiting for 1 "); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized (r2) { System.out.println(" i m in resource 1 "); } } } }; t1.start(); t2.start(); }}INPUTOUTPUTi m in resource 1 , waiting for 2 i m in resource 2 i m in resource 2 , waiting for 1 i m in resource 1

0 comments :
Post a Comment