• 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

Threads and object locking

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following keywords indicates a thread is releasing its Object lock?
1) release
2) wait
3) continue
4) notifyAll
Answer: #2
Isn't this when the thread is 'picking up' the object thread? Please explain....
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds to me a very ambigous question.
(1) As far as I know, there is no release method related to Thread
(2) wait : This method is used when you want to put the 'running' thread into 'not running' state (in the wait pool)
(3) continue : Is a keyword in Java, nothing to do with locks
(4) notifyall: Used to signal thread(s) from the waiting pool to move to 'ready to run' state.
-Sam
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi:
When a thread executes the command wait, it releases the lock of the object it had acquired.
The command wait can occur only in synchronized code. This means before executing wait, the thread would have acquired an object lock for sure. Now when the thread calls wait, it releases this lock and enters a pool of waiting threads (managed by the object whose wait method was called).
It tries to get back the lock only after it has been notified. This notification is done using notify/notifyAll which must again be in a synchronized code. As notify arbitrarily picks up a waiting thread, to ensure that this notification is useful, notifyAll may be used which will notify all the waiting threads.
Gaja.
 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

wait(),notify(), notifyAll() are only usable in synchronized methods and synchronized code blocks.


Any exception of this quotation?
And plz tell me something abt synchronizing classes or class locks.
------------------
azaman

[This message has been edited by Ashik uzzaman (edited August 16, 2001).]
 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both wait() and notify() indicate that a thread is releasing its Object lock. as both of them are to be used within a synch block/method, the object is already locked.
- wait() releases the lock and goes in ready/sleep state
-notify() also releases the lock and notifies the threads that r in ready state and waiting for this object lock to be released.
I hope i am correct.
Rashmi
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A thread that calls wait() releases the virual cpu, and at the same time it releases the lock.
when the thread is notified one chosen threat gets moved out of the monitor's waiting pool and int o the seeking lock state and if the lock is there it reaquire it.
ronak
 
Willie Toma
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can wait() and notify() both release the lock? Isn't one locking (Pausing) and the other unlocking (un-pausing)?
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


How can wait() and notify() both release the lock? Isn't one locking (Pausing) and the other unlocking (un-pausing)?


No, wait() does not lock the object, it releases it.(Reference to khalid mughal & Rasmussen Book)


While in waiting state, thread relinquishes the ownrship of monitor of the object.Transition to waiting state and relinquishing the object monitor are completed as one automic operation.


notify() is used to move threads(waiting for object lock to be realesed) from waiting state to ready state. Therefor notify() also releases lock obtained using synch.
Rashmi
 
Then YOU must do the pig's work! Read this tiny ad. READ IT!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic