File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Doubt" Watch "Thread Doubt" New topic
Author

Thread Doubt

Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Consider the following method:



and the following instantiations:



obj1 and obj2 are accesible to two different threads and the threads are about to call the getLocks() method.
Assume the first thread calls the method getLocks(obj1, obj2).

Which of the following is true?

Options

Select 1 correct option.


Ans: The second thread should call getLocks(obj1, obj2)

Doubt: i think that "The second thread may call getLocks() any time and passing parameters in any order." is correct. Could someone clarify my doubt.

Source: (Question ID: 1054) Enthuware

Thanks,
Gitesh
[ March 21, 2008: Message edited by: Gitesh Ramchandani ]
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Can someone help me with the above posted doubt?

Thanks,
Gitesh
Amrit Kashyap
Ranch Hand

Joined: Apr 23, 2006
Posts: 44
Hi Gitesh,

Can you tell what is your exact question?

From the answer, which you are saying to be correct, the question should be "How thread2 can avoid any chances of deadlock situation with thread1?"

Correct me if i am getting it wrong?

If that is the question, then your answer option 4, may result in a deadlock situation, if the order of parameter passed is obj2 and obj1.

I think option 3 is also right.

"The second thread should call getLocks() only after first thread exits out of it. The is not necessary. Option 2 works just fine."

When thread1 has exited from getLocks(), thread2 can safely call getLocks() giving parameter in any order.
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Hi Amrit,
i'm repeating the question again here:

obj1 and obj2 are accesible to two different threads and the threads are about to call the getLocks() method.
Assume the first thread calls the method getLocks(obj1, obj2).

Which 1 of the following options is true?

Options




From the answer, which you are saying to be correct, the question should be "How thread2 can avoid any chances of deadlock situation with thread1?"

Correct me if i am getting it wrong?


No, the question is not about avoiding deadlock.

Hope someone helps with the question :roll:

Regards,
Gitesh
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

No, the question is not about avoiding deadlock.


Then, what is the question about? Option 1 & option 4 can cause a deadlock.

Henry
[ March 21, 2008: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1836
    
    7

It most certainly does have to do with avoiding a dead-lock situation.

Option 1 could result in dead-lock, due to unlucky timing.
If thread 1 would call getLocks(A, B), and thread 2 would call getLocks(B, A) then thread 1 (while running) could obtain the lock on A and be moved to runnable state by the thread schedular. Thread 2 could then be moved to running state by the thread schedular and obtain the lock on B. From here on out it no longer matters which thread continues to run, because thread 1 owns the lock on A and thread 2 owns the lock on B. Both threads are blocked because they both need the lock held by the other thread.
Enter the horror that is dead-lock. Which also disqualifies option 4.

Option 2 could not result in dead-lock, because even if the thread schedular performs some nasty scheduling logic, which ever thread executed getLocks(A, B) first, will own the lock on A and block the other thread from ever obtaining the lock on B. Dead-lock avoided. Disqualifies option 3 and option 5, because this is the single valid option we were looking for. Option 3 could not be guarenteed anyway, due to the unpredicability of thread schedular magic, as the getLocks() method itself is not synchronized using the intrinsic lock of the object it is called on.
[ March 21, 2008: Message edited by: Jelle Klap ]

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Thanks Amrit, Henry and Jelle

I happen to miss the deadlock condition in the question, but am clear on the question now.

Thanks,
Gitesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Doubt