• 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

Thread sleep() method doubt

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

K&B book page 730 says:

"If you want a guarantee that your threads will take turns regardless of underlying JVM, you can use the sleep() method. This prevents one thread from hogging the running process while another thread starves." //1

and next it says that "Sleeping is used to delay execution for a period of time, and no object lock is released when thread goes to sleep."

So , my question is, if Thread.sleep() do not release object lock then how can another thread execute ? Or how another thread will start processing and not get blocked?


From sentence 1 I understand that when Thread goes to sleep it will stop executing for a period of time but at the same time it is not giving up processor to some other thread and it will continue running after time expires.

Can someone explain me whats exactly behavior of sleep?

- Vivian
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Answer lies within your question.

If one threads free's processor any thread will gain the control(based on priority) which is not waiting on any object lock and is in que to be executed and waiting for the processor cycle.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vivian Josh:
... From sentence 1 I understand that when Thread goes to sleep it will stop executing for a period of time but at the same time it is not giving up processor...


A sleeping thread does give up the processor. It does not give up the lock (if it has one).

Note that locks are only a consideration with synchronized code. If a thread is executing synchronized code, then putting it to sleep won't help much if other threads need that same lock.
 
Vivian Josh
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks marc & subodh !

Yes, I know I was little confused on those words. But know with marc's explanation of 'processor acquisition' and 'object lock' it is more clear.

Thanks again.
reply
    Bookmark Topic Watch Topic
  • New Topic