• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Doubt on Thread.sleep()

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quote from Java Tutorial rocessing time for a single core is shared among processes and threads through an OS feature called time slicing.

It seems when one thread runs out its time slicing, the OS will let other threads get to run. Why I need bother to invoke Thread.sleep() method to transfer the CPU time to other threads?

Thanks.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Louis:



It seems when one thread runs out its time slicing, the OS will let other threads get to run. Why I need bother to invoke Thread.sleep() method to transfer the CPU time to other threads?



hi,

While developing mulithreaded application, developer should not consider what mechanism OS follow, time slicing, preemptive, no preemptive or whatever. How can we be assured that what time slicing the CPU has given to particular thread. So therfore we programatically do Thread.sleep(1000) or so, that we could be assured that while thread executes this line, it sleeps. One more thing, if a thread sleeps it does not leave the object so that lock or synchronized code can't be accessed by another thread while the prior one is sleeping.



Thanks and Regards,
cmbhatt
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic