aspose file tools*
The moose likes Threads and Synchronization and the fly likes Thread.sleep vs yield (again) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread.sleep vs yield (again)" Watch "Thread.sleep vs yield (again)" New topic
Author

Thread.sleep vs yield (again)

Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
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


Books: Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
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

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread.sleep vs yield (again)