======================================================================== 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
posted
0
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. Rgds Sahir
....
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 3901
posted
0
the correct answers are b and d. the exam is wrong.
I never took notes in college. That's how I got a 4.0 the first 2 years, and a 3.5 the second two years.
deekasha gunwant
Ranch Hand
Joined: May 06, 2000
Posts: 396
posted
0
hi minakshi, randall is right.only b,d are correct. anbd c must be a typo as sleeping thread does not releases any locks. regards Deekasha
bill bozeman
Ranch Hand
Joined: Jun 30, 2000
Posts: 1070
posted
0
As everyone else said, b and d are correct. I am moving this to the mocks errata forum. Bill
Minakshi Jain
Greenhorn
Joined: Dec 11, 2000
Posts: 4
posted
0
Thanks for clarifying. Minakshi
sirisha parimi
Greenhorn
Joined: Dec 18, 2000
Posts: 2
posted
0
hi, i think b , cand d are correct . c--- as if any thread wants to access , it can only when it gets the lock. b-- it has to sleep for min of 500ms untill it is interrupted. d-- 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
posted
0
sleep() method doesn't release the lock, so C is not a correct answer. Bill
urja patel
Greenhorn
Joined: Dec 21, 2000
Posts: 1
posted
0
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
posted
0
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. Bill