Page 1 of 1

About Question enthuware.ocpjp.v7.2.1482 :

Posted: Sun Oct 27, 2013 7:57 am
by kumarkhiani
In such a case, If we first try to get a lock on obj1 in both m1() and m2() - why the deadlock cannot be avoided?

Code: Select all

public void m1()
{
   synchronized(obj1)
   {
     System.out.print("1 ");
     synchronized(obj2)
     {
       System.out.println("2");
     }
   }
}
public void m2()
{
   synchronized(obj1)
   {
     System.out.print("2 ");
     synchronized(obj2)
     {
       System.out.println("1");
     }
   }
} 

i tried the above code and it does results in a deadlock as well.

Re: About Question enthuware.ocpjp.v7.2.1482 :

Posted: Sun Oct 27, 2013 11:32 am
by admin
I am not sure I follow your question. The code that you've shown above acquires both the locks in the same order. So there can never be a situation where thread 1 has lock for obj1 and thread 2 has a lock for obj2 (or vice versa).

Please check that code that you are actually running.

You might want to read this as well: https://www.securecoding.cert.org/confl ... same+order

-Paul.

Re: About Question enthuware.ocpjp.v7.2.1482 :

Posted: Mon Oct 28, 2013 10:24 pm
by kumarkhiani
Thanks for your reply.

It indeed does NOT get into a deadlock if the synchronization is called in the same sequence on the two objects.

Re: About Question enthuware.ocpjp.v7.2.1482 :

Posted: Thu Apr 27, 2017 5:19 am
by nikitos
What means "Note that the threads are not dead."?

Re: About Question enthuware.ocpjp.v7.2.1482 :

Posted: Thu Apr 27, 2017 5:38 am
by admin
It means they are not in the TERMINATED state.