About Question enthuware.ocpjp.v7.2.1482 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
kumarkhiani
Posts: 9
Joined: Sun Aug 25, 2013 2:36 am
Contact:

About Question enthuware.ocpjp.v7.2.1482 :

Post 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.

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

kumarkhiani
Posts: 9
Joined: Sun Aug 25, 2013 2:36 am
Contact:

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

Post 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.

nikitos
Posts: 21
Joined: Mon Oct 24, 2016 6:55 am
Contact:

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

Post by nikitos »

What means "Note that the threads are not dead."?

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

It means they are not in the TERMINATED state.

Post Reply

Who is online

Users browsing this forum: No registered users and 22 guests