Forums Register Login

Thread.sleep vs yield (again)

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 8145 times.
Similar Threads
Marcus Mock Question 24
Sleep and yield methods
Animating an object between two points
Sleep or Yield in a infinite loop
Thread with synchronized run() method and sleep()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 01:11:26.