java - When I run this code, the application does not terminate -


public class threaddemo {  public static void main(string[] args) throws interruptedexception {  object obj1 = new object();  object obj2 = new object();  object obj3 = new object();  thread t1 = new thread(new syncthread(obj1, obj2), "t1");  thread t2 = new thread(new syncthread(obj2, obj3), "t2");  thread t3 = new thread(new syncthread(obj3, obj1), "t3");  t1.start();  thread.sleep(500);  t2.start();  thread.sleep(500);  t3.start();  }  }  class syncthread implements runnable{  private object obj1;  private object obj2;  public syncthread(object o1, object o2){  this.obj1=o1;  this.obj2=o2;  }  @override  public void run() {  string name = thread.currentthread().getname();  system.out.println(name + " acquiring lock on "+obj1);  synchronized (obj1) {  system.out.println(name + " acquired lock on "+obj1);  work();  system.out.println(name + " acquiring lock on "+obj2);  synchronized (obj2) {  system.out.println(name + " acquired lock on "+obj2);  work();  }  system.out.println(name + " released lock on "+obj2);  }  system.out.println(name + " released lock on "+obj1);  system.out.println(name + " finished execution.");  }  private void work() {  try {  thread.sleep(30000);  } catch (interruptedexception e) {  e.printstacktrace();  }  }  } 

you have deadlock problem.

t1 aquires lock obj1. work. in mean time t2 aquires lock obj2 , work. same goes t3 , obj3.

now t1 done first work wants aquire lock of obj2. has wait until t2 finished obj2. t2 waiting t3 finish , release lock obj3. t3 on other hand release lock obj3 if has lock of obj1, blocked t1.

so waiting other 1 finish. definition of deadlock 3 threads.

you can see in output:

t1 acquiring lock on java.lang.object@78f92cc8                                                                                                                                                                                     t1 acquired lock on java.lang.object@78f92cc8                                                                                                                                                                                      t2 acquiring lock on java.lang.object@55d2162c                                                                                                                                                                                     t2 acquired lock on java.lang.object@55d2162c                                                                                                                                                                                      t3 acquiring lock on java.lang.object@47122d                                                                                                                                                                                       t3 acquired lock on java.lang.object@47122d                                                                                                                                                                                        t1 acquiring lock on java.lang.object@55d2162c (<-- waiting t2 finish)                                                                                                                                                                                     t2 acquiring lock on java.lang.object@47122d (<--waiting t3 finish)                                                                                                                                                                                       t3 acquiring lock on java.lang.object@78f92cc8 (<-- waiting t1 finish) 

hope you.

regards, chris


Comments