i trying achieve deadlock condition in program program not giving deadlock condition .why? please tell me problem in code.
the output :
thread1 : locked integer
thread2 : locked string
thread1 : locked string
thread2 : locked integer
code:
class dead { public void meth1() { synchronized (integer.class) { system.out.println("thread 1: locked integer"); } try { thread.sleep(5000); } catch (interruptedexception e) { system.out.println(e); } synchronized (string.class) { system.out.println("thread 1: locked string"); } } public void meth2() { synchronized (string.class) { system.out.println("thread 2: locked string"); } try { thread.sleep(5000); } catch (interruptedexception e) { system.out.println(e); } synchronized (integer.class) { system.out.println("thread 2: locked integer"); } } } class mthread1 extends thread { dead d; mthread1(dead d) { this.d = d; } public void run() { d.meth1(); } } class mthread2 extends thread { dead d; mthread2(dead d) { this.d = d; } public void run() { d.meth2(); } } public class thread1 { public static void main(string[] args) { // dead d = new dead(); mthread1 t1 = new mthread1(new dead()); mthread2 t2 = new mthread2(new dead()); t1.start(); t2.start(); } }
when write
synchronized (integer.class) { system.out.println("thread 1: locked integer"); } it locks integer.class object , unlocks it. want this:
synchronized(integer.class) { system.out.println("thread 1: locked integer"); try { thread.sleep(5000); } catch(interruptedexception e) { system.out.println(e); } synchronized(string.class) { system.out.println("thread 1: locked string"); } }
Comments
Post a Comment