Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

wait() and lock

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

First of all I should thank you once agian for helping me in the problems I posted in the past.

Here I am dealing with one confusion in threads and want help and appreciate it in advance if somebody assists.

When we call wait method on the object then only the lock of that Object is released(Khalid Mughal's one of the query)

Select the two correct answers.

a) A thread can hold more than one lock at a time.

b) Invoking wait() on a Thread object will relinquish all locks held by the thread.

c)Invoking wait() on an object whose lock is held by the current thread will relinquish the lock.

d)Invoking notify() on a object whose lock is held by the current thread will relinquish the lock.

e)Multiple threads can hold the same lock at the same time.

Answer a,c

I was doing the Master test from K&B book CD, one of the answer is:

When a thread is waiting as a result of wait(), it releases all the locks it holds. while option c above contradicts it.

Please help

Uzma
 
author
Posts: 23919
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

When a thread is waiting as a result of wait(), it releases all the locks it holds. while option c above contradicts it.



Obviously, this statement is untrue, as it will make the wait() method pretty dangerous to use -- free locks used by Swing, JDBC, etc.

However, the book may be referring to something else. A thread is allowed to acquire a lock that it already owns. This is needed for a synchronized method to call another synchronized method of the same instance. The wait() method will release the lock completely, meaning all the times it acquired it.

Henry
 
uzma Akbar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Wong for clearing up my concept.

The test I was doing from the CD comes with the K&B book so was wondering as my concept was not going side by side. If you have an access to CD then please check what I had mentioned in the Master exam.

Thanks again!
Uzma
 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic