• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Sleep or Yield in a infinite loop

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sleep or Yield in a infinite loop
I need to code an infinite loop. What is the best way to pause
the thread for a while.
Sleep : The thread would go to the "sleep" state for the specified amount of time.
Yield: The thread would move from running state to ready state

run()
{
while(true)
{
//do something
Thread.sleep(1000);
OR
Thread.yield();
}
}
Does yield have any problem in a preemptive scheduling system.(like NT)
Thanks
Gobiraj
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my experience sleep is the way to go. The yeild loops I've coded tend to be huge processor hogs. I've had to replace them all in the program I've been developing.
The sleep value should be significantly large as well. 1 sec or more.... if possible of course.
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"vgobiraj",
your name is not valid as per the Javaranch name conventions. Please refer to the document located here
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic