JDK-4799163 : Thread.yield() NOP implementation prerequsites
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux
  • CPU: x86
  • Submitted: 2003-01-06
  • Updated: 2006-08-07
  • Resolved: 2006-08-07
Related Reports
Relates :  
JVM implementations that do not map Java thread priorities to system thread priorities must not implement Thread.yield as NOP. It does not allow to properly implement background activity (for performance reasons).

I saw behaviour that looks like NOP implementation on Linux and Solaris implementations. (On windows it's probably NOP too but it does not matter as it maps thread priorities).


LOWERed priority, this bug is workaround for 4813310.

###@###.### 2003-03-24
Not-a-bug.  See the comments in http://java.sun.com/j2se/1.5.0/docs/guide/vm/thread-priorities.html.  

The Thread.setPriority and Thread.yield methods are advisory. They constitute hints from the application to the JVM. Properly written, robust, platform-independent code can use setPriority() and yield() to optimize the performance of the application, but should not depend on these attributes for correctness. Likewise, no assumptions should be made about the order in which threads are granted ownership of a monitor or the order in which threads wake in response to the notify or notifyAll method. An excellent reference for these topics is Chapter 9, "Threads," in Joshua Bloch's book Effective Java Programming Language Guide.

WORK AROUND Thread.sleep(1);

PUBLIC COMMENTS Thread,yield() must not be implemented as NOP on cooperative OSes and all JVM implementations that do not map Java thread priorites to OS level thread priorities. Otherwise it causes performance problems while trying to implement backgroud activity. It is pretty serious for client side applications.

EVALUATION On Linux and Windows, java.lang.Thread.yield() is translated directly into OS yield calls (sched_yield() on Linux and Sleep(0) on Windows). If you are running on Redhat 8, you might see behavior that looks like yield is implemented as NOP, but that's a problem in Redhat kernel, please see bug 4790855. On Solaris, we prevent frequent yielding between threads by changing yields to NOPs if they happen in less than 10ms intervals. You can disable this behavior by "-XX:-DontYieldALot". Note that frequent yielding among threads can add significant overhead due to unnecessary context switches. However, it does look like the Solaris behavior has some problems. First, it seems in violation of the definition of java.lang.Thread.yield(), which should "cause the currently executing thread object to temporarily pause and allow other threads to execute"; second, the 10ms limit is across the process and doesn't consider the number of CPUs. ###@###.### 2003-01-06 Yes I tested on RedHat 8.0. Comment on note: I expected Thread.yield() behaves as you described for Solaris. That it batches request per thread and propagates to OS context switch call after some timeout. Besides according to JVM spec well behaved code must use Thread.yield() occasionally because preemptive threading model is not guarateed. ###@###.### 2003-01-07 While running on stock Linux 2.4.20 I get better results. Now I miss only Solaris batching logic :-). ###@###.### 2003-03-04