• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing locks from one thread to another.

 
Arnob Dey
Greenhorn
Posts: 28
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scenario:

Thread 1 currently holds the locks for an object(obj) for which 4 other threads, are waiting. Now, thread 1 wants to release the lock but at the same time, it wants thread 3 to get the lock. How will you accomplish this?

Simple answer is: It's simply not possible. Thread can only release the lock and it has no control over who gets the lock next.

Now the stupid question is: What about Thread.join()? How that works out?
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arnob Dey wrote:
Simple answer is: It's simply not possible. Thread can only release the lock and it has no control over who gets the lock next.

Now the stupid question is: What about Thread.join()? How that works out?


Not exactly sure how it is that related, meaning the two points in your topic. Thread join() is more related to the use of the wait and notify mechanism, than about the synchronization lock required by the mechanism.

Anyway, when the thread has completed (and is no longer alive), one of the cleanup tasks is to send a notifyAll(). And once this call releases the lock, it doesn't really care who gets control of the lock next. They will all take turns getting the lock, waking up, releasing the lock, and returning from the join() method.

Henry
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thread 1 currently holds the locks for an object(obj) for which 4 other threads, are waiting. Now, thread 1 wants to release the lock but at the same time, it wants thread 3 to get the lock. How will you accomplish this?


Simple answer is: It's simply not possible. Thread can only release the lock and it has no control over who gets the lock next.


That's not strictly true. There is a way a thread can have some control over which thread will acquire the lock next. However why do you want to do this? Why do you want thread 3 to get priority?
 
Arnob Dey
Greenhorn
Posts: 28
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Mike.J.Thompson

This was actually a OCPJP7 mock question.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, well if you restrict the meaning to very strictly only locking and unlocking Locks then they are correct. The thread that is unlocking the Lock can't decide which thread will receive the lock next.

However if you use Locks and Conditions then you can affect which thread will get the Lock next.
 
Arnob Dey
Greenhorn
Posts: 28
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Mike. J. Thompson

ya that's true. We can use conditions to affect which thread will get the lock but I guess that's not the sole intended purpose of conditions...:-)


On the other hand, I do agree with Mr.Henry Wong though. Thread.join() has nothing to do with Locking mechanism, these are from entirely different context. I was initially a bit confused.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!