Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Doubt

 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone help me with the above posted doubt?

Thanks,
Gitesh
 
Amrit Kashyap
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 20834
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic