• 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 vs yield (again)

 
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've searched a bit (here, JGuru, elsewhere) but have not found a clear answer on this.
Running a tight loop under Windows, Thread.yield() appears to be a no-op--I can observe in the task manager that java is pegging the CPU at close to 100%. This agrees with what I've found by searching--in a preemptive multitasking OS, yield does nothing. Thread.sleep(0) has the same result.
Thread.sleep(1), however, appears to be the way to go--CPU usage by Java stays down.
When, then, would use of yield be preferable--only in a cooperative environment? If one is writing multiplatform code, wouldn't Thread.sleep(1) be the way to go?
Thanks,
Jeff
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're confusing multithreading and multitasking. yield() only promises to offer to cede control to another runnable thread in the same JVM, not in some other process. sleep(1) does nothing for a millisecond, so yes, CPU utilization will go down.
 
Jeff Langr
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
I think you're confusing multithreading and multitasking. yield() only promises to offer to cede control to another runnable thread in the same JVM, not in some other process. sleep(1) does nothing for a millisecond, so yes, CPU utilization will go down.


I think you're right, but I'm still not understanding. Why would I ever want to use yield over sleep(1), since sleep should also cede control to another thread (or does it not?)?
thanks,
Jeff
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeff Langr:

I think you're right, but I'm still not understanding. Why would I ever want to use yield over sleep(1), since sleep should also cede control to another thread (or does it not?)?


If you sleep(), the sleeping thread will do nothing for the duration of the sleep, even if no other thread is runnable. If no other process wants the CPU, either, then the CPU will go idle.
In contrast, if you yield() when no other thread is runnable, the yielding thread will carry on processing almost straight away.
By the way, it is unlikely that sleep(1) does actually sleep for only 1 millisecond. The operating system, unless it's a proper real-time one, probably doesn't have such fine resolution. Thus, sleep(1) will probably suspend your thread for several milliseconds, which is a really long time.
 
Jeff Langr
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter Chase:

If you sleep(), the sleeping thread will do nothing for the duration of the sleep, even if no other thread is runnable. If no other process wants the CPU, either, then the CPU will go idle.
In contrast, if you yield() when no other thread is runnable, the yielding thread will carry on processing almost straight away.
By the way, it is unlikely that sleep(1) does actually sleep for only 1 millisecond. The operating system, unless it's a proper real-time one, probably doesn't have such fine resolution. Thus, sleep(1) will probably suspend your thread for several milliseconds, which is a really long time.


Thanks for the distinctions. I was aware of the granularity issue. In my case, perhaps I should be looking at a wait/notify mechanism instead. I note that the BlockingQueue implementations in 1.5 don't have the problem of hogging the processor.
Regards,
Jeff
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic