# HG changeset patch # User bharadwaj # Date 1461265440 14400 # Node ID f3909e996a6a4729373c4f5ac404ec12b7f8044c # Parent ccb5cb0c813275680d74c3d277170fbc71d05e61# Parent 8aea6082752407f62fc81d65138b5bb91627d8f2 Merge diff -r ccb5cb0c8132 -r f3909e996a6a jdk/src/java.base/share/classes/java/lang/Thread.java --- a/jdk/src/java.base/share/classes/java/lang/Thread.java Thu Apr 21 13:18:46 2016 +0300 +++ b/jdk/src/java.base/share/classes/java/lang/Thread.java Thu Apr 21 15:04:00 2016 -0400 @@ -341,6 +341,45 @@ } /** + * Indicates that the caller is momentarily unable to progress, until the + * occurrence of one or more actions on the part of other activities. By + * invoking this method within each iteration of a spin-wait loop construct, + * the calling thread indicates to the runtime that it is busy-waiting. + * The runtime may take action to improve the performance of invoking + * spin-wait loop constructions. + *

+ * @apiNote + * As an example consider a method in a class that spins in a loop until + * some flag is set outside of that method. A call to the {@code onSpinWait} + * method should be placed inside the spin loop. + *

{@code
+     *     class EventHandler {
+     *         volatile boolean eventNotificationNotReceived;
+     *         void waitForEventAndHandleIt() {
+     *             while ( eventNotificationNotReceived ) {
+     *                 java.lang.Thread.onSpinWait();
+     *             }
+     *             readAndProcessEvent();
+     *         }
+     *
+     *         void readAndProcessEvent() {
+     *             // Read event from some source and process it
+     *              . . .
+     *         }
+     *     }
+     * }
+ *

+ * The code above would remain correct even if the {@code onSpinWait} + * method was not called at all. However on some architectures the Java + * Virtual Machine may issue the processor instructions to address such + * code patterns in a more beneficial way. + *

+ * @since 9 + */ + @HotSpotIntrinsicCandidate + public static void onSpinWait() {} + + /** * Initializes a Thread with the current AccessControlContext. * @see #init(ThreadGroup,Runnable,String,long,AccessControlContext,boolean) */