• 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

K&B, Q. 14, Threads

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which two r true ?
One of the answer is
d) When a thread is waiting as a result of wait(), it release its locks.
Is it that if a thread is holding multiple locks and it calls wait() on an object, then all the locks r released or only one of them is released ?


try
{
synchronized(a)
{
synchronized(b)
{
b.wait();
}
}
}
catch(InterruptedException){}


How many locks r released ?
Locks on
1. b only
2. Both a and b
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only b.
 
Gopal Shah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the answer is wrong.
Because the answer given is
d) When a thread is waiting as a result of wait(), it release its locks.
Instead it should have been

d) When a thread is waiting as a result of wait(), it releases its lock.
I understand it is a very small margin of error but actually in the present context the meaning of the statement totally changes.
Probably this should never be the objective of SCJP. But after taking some of the mock tests, I have been virtually checking each and every word.
Sometimes the question looks very straight forward but actually when u check the answer it is different.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should have said that it releases its locks on that object. By executing nested synchronized sections of code, a thread can pick up multiple locks on a single object. It lets tham all go when it calls wait() on that object, and then picks them all back up when it returns from wait(). But of course, it keeps any locks it has on other objects. So the question is ambiguous as stated.
[ September 27, 2003: Message edited by: Steve Lovelace ]
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
posted by Steve:

It should have said that it releases its locks on that object. By executing nested synchronized sections of code, a thread can pick up multiple locks on a single object. It lets tham all go when it calls wait() on that object, and then picks them all back up when it returns from wait(). But of course, it keeps any locks it has on other objects. So the question is ambiguous as stated.


Multiple locks on a single object?
I thought there is one lock for every Java object....
Barkat
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Multiple locks actions on the same lock.
For instance:
synchronized void methodA() {
methodB();
}
synchronized void methodB() {
try { wait(); }
catch(InterruptedException e) {}
}
A thread beginning to execute code in methodB --being called from methodA-- would have adquired this' lock twice. wait will perform two unlock actions and when notified two locks actions; all of them on this' lock.
 
reply
    Bookmark Topic Watch Topic
  • New Topic