Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Yield's and Sleep()  RSS feed

 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In reading Java Threads, it becomes apparent that my original Threading strategy is the likely best strategey. On a uniprocessor machine, a yield or sleep() may not do much good. On a higher-end machine, my orginal strategy would be paralleled by a parallelizing compiler. The nature and design of the program is such that the number of files open and the file sizes may vary in dramatic or routine ways - my solution was to write a class that would process these files and fire off one in a loop for each file returned by a directory listing.

In that the machine may be loaded at some point, I decided to put thread count management and priority management in main() - all of the compilers I have seen for winnie define away yield() - the correct way to do a yield being sleep(0);

We cannot make assumptions about the scheduler, and the actions of the scheduler on a uni-processor machine - to me - add further fog to the issue that the disk heads are molasses compared to processing involved ..... but given my practical experience that many solutions are the mirror image of the first solution, I wanted to ask before I dream up some initial load-balancing tests whether Yield() is actually implemented in the JVM - whether by request to the OS or not - and what the effect of a sleep(0); would be. (by design of the JVM)

Book says 20-50 ms sleeps are what actually happens, which is fine for a point to begin testing, or contemplating tests, under my original design ideas.
//eof
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a question?
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Yield() is actually implemented in the VM ?
  • What is the effect of sleep(0); ?
  •  
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Both yield() and sleep() are implemented in native code. (You can see the Java source for standard libraries in the src.zip or sjr.jar file which should be found in your installed JDK, unless you declined this option when you installed). In terms of the specification, yield() and sleep(0) may have exactly the same effect (which may legally be, none at all) but in terms of what they actually do do, that's up to the JVM implementation. If you really want to know exactly how they work, you'll probably need to dig into the C source code of the particular JDK you're using (assuming it's available). Or write some careful performance tests to see if there's any observable distinction between sleep(0) and yield().

    From my own experience, I've occasionally found I needed[i] yield(), and I've never observed any [i]problems because of yield(). (Never bothered with sleep(0), but I'd expect it to have the same effect.) In general I consider it good practice to insert at least one yield() anytime I see a thread doing a potentially lengthy loop with no sleep() or wait() already present in the loop.
     
    Nicholas Jordan
    Ranch Hand
    Posts: 1282
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you so much for your discussion of the issue.

    Since posting, I have finished first reading of Java Threads. Generally, things are largely as I suspected, and fine-grained testing may reveal answers to my thoughts.

    I unpacked rt.jar using tools as instructed in the documentation, but am not able to effectively use the C-Sources at my current skill level.

    I did read Thread.java in it's entireity.

    My original design philosophy was to put some yields around to avoid starving other process on a uni-processor machine, notably where you mentioned.

    And for matters normally addressed in discussion of threading issues.

    I take it from your instruct that if my fingers are acclimatized to coding sleep(0); where a yield might go - the general intent in the VM works toward a similar end result.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!