• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread.sleep vs yield (again)

 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • 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
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic