• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Doubt

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone help me with the above posted doubt?

Thanks,
Gitesh
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Amrit, Henry and Jelle

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

Thanks,
Gitesh
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic