• 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

Passing locks from one thread to another.

 
Ranch Hand
Posts: 43
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
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

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
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 43
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 43
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic