Two Laptop Bag
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads" Watch "Threads" New topic


Arvind Kini
Ranch Hand

Joined: Dec 28, 2000
Posts: 31

Please excuse me if this question has been discussed. tried doin a search but then cudnt help myself!
Read the following snippet carefully
1. public synchronized void someMethod() {
2. //lots of code
3. try {
4. Thread.sleep(500);
5. } catch(InterruptedException e) {
6. //do some things here.
7. }
8. //more and more code here
9. }
Select all correct answers
a) code causes compilation error -sleep cannot be called in synchronised methods
b) thread sleeps for atleast 500 ms in this method if not interrupted
c) When the thread goes to sleep it releases lock on the object.
d) the sleeping thread alwayz have lock on the object.
ans given c,d
according to me it shud have been only b)
but then went thru khalid and it says c is right too
but how d) can be possible if c is true?
Please help

Rob Acraman
Ranch Hand

Joined: Dec 03, 2000
Posts: 89
The API documentation for Thread.sleep states:
"The thread does not lose ownership of any monitors."
ie. it does not lose the lock.
Therefore, it seems to me that b and d are the correct answers.
What's the URL for the exam?
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Arvind,
The correct answers are C and D.

Sorry folks ..typo .. should read B and D

<code>sleep()</code> does not give up the lock. See this discussion and answer by Ajith.
Hope that helps.
[This message has been edited by Jane Griscti (edited January 16, 2001).]

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Amit, Jhalani

Joined: Jan 11, 2001
Posts: 21
When sleep() did'nt releases the lock then how come 'C' is the correct answer?
Rajpal Kandhari
Ranch Hand

Joined: Aug 26, 2000
Posts: 126
Hello Arvind,

From whcih mock exam you got this question. Pls put up the link to that mock site and this question is ment for errata Forum.

Regards,<P>Raj.<BR>-------------------------<BR>Afforts should be Appriciated.<BR>-------------------------
denish mehta
Ranch Hand

Joined: Dec 21, 2000
Posts: 135
how can u have the lock and not have it at the same time??
if both c & d are correct then won't we have a paradox??
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Sorry folks ...
Typo ... the correct answer should be B and D, not C and D.
Apologies for the confusion.


Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
Arvind Kini
Ranch Hand

Joined: Dec 28, 2000
Posts: 31
I am sorry guys, I donot rememebr the place from where I have taken this question . wud look out anyway .
So the confirmed ans is B and D.
Thankz to all
I agree. Here's the link:
subject: Threads
It's not a secret anymore!