• 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

One Question about Thread

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,look at code below:
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 compiling error--can not call sleep inside synchronized method
B.The thread sleep at least 500 milliseconds if not interrupted
C.When the thread goes to sleep,it releases the lock on the object
D.The "Sleeping" thread always have the lock on the Object
Answer:C,D
I think C,D is contradictory.
Why C,D is right while B is wrong.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Richard
Once you call sleep the thread releases the lock on the object. The thread is in the blocking state. If the thread is not interrupted it will sleep for 500 milliseconds before going back to the ready state.
Not 100% sure but it seems the correct answer is
B,C
regards
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NO NO NO!
Calling sleep() does NOT release the lock. ONLY wait() releases the lock and puts the Thread in a special wait state.
B and D are correct.
Bill
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually i think u r wrong Stephen.
here is what the API says:

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors.


so it doesnt give up the monitor. d is right therefore. and not c.also i think b is right.
 
reply
    Bookmark Topic Watch Topic
  • New Topic