It's not a secret anymore!
The moose likes Mock Exam Errata and the fly likes question from Jquest on Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "question from Jquest on Threads" Watch "question from Jquest on Threads" New topic

question from Jquest on Threads


Joined: Nov 28, 2000
Posts: 4
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. The code causes compilation error - sleep cannot be called inside synchronous methods
b. The thread sleeps for atleast 500 milliseconds in this method if not interrupted.
c. When the thread "goes to sleep" it releases the lock on the object.
d. The "sleeping" threads always have the lock on the object.
The answer is given as 'c' and 'd' which seems contradictory to me. I think it should be 'b' and 'c'
Please clarify.
Sahir Shah
Ranch Hand

Joined: Nov 05, 2000
Posts: 158

C is wrong. Is probably a typo .
A sleeping thread does not lose ownership of any monitors it had aquired. What is JQuest? Is this a mock exam? If so it would
help other candidates if you post this at the mock exam errata forum.

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

the correct answers are b and d.
the exam is wrong.

Visit my download page
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
hi minakshi,
randall is right.only b,d are correct.
anbd c must be a typo as sleeping thread does not releases any locks.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
As everyone else said, b and d are correct.
I am moving this to the mocks errata forum.
Minakshi Jain

Joined: Dec 11, 2000
Posts: 4
Thanks for clarifying.
sirisha parimi

Joined: Dec 18, 2000
Posts: 2
i think b , cand d are correct .
as if any thread wants to access , it can only when it gets the lock.
it has to sleep for min of 500ms untill
it is interrupted.
unless another thread is there waiting it gets notified.otherwise it sleeps.

[This message has been edited by sirisha parimi (edited December 19, 2000).]
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
sleep() method doesn't release the lock, so C is not a correct answer.
urja patel

Joined: Dec 21, 2000
Posts: 1
Actually the reply is
b and c..
b) because,it sleep for atleast 500 milliseconds.
Not less than it.
c) because,as it is in synchronized block,it releases
lock as soon as it goes to sleep.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
urja, that is not correct.
Sleeping threads DO NOT release the locks.
Here is an example to prove my point:

The second thread will never start until the first thread is finished because the first thread gets the lock and doesn't give it up. Change sleep() to wait() and you will see that the new thread will start right after the first thread hits wait(). The program will deadlock on you since both threads are waiting and there is no notify() statement, but it still gets the point across.
I agree. Here's the link:
subject: question from Jquest on Threads
It's not a secret anymore!