onSpinWait Java 9

Asked

Viewed 57 times

2

I am porting a Java 8 application to 9, I have some processes that use the concept of Watchdog, who use something similar to:

public synchronized void run() {
    until = System.currentTimeMillis() + watchdogParam.getTimeout();
    while (!cancelado) {
        long delta = until - System.currentTimeMillis();
        try {
            if (delta > 0) {
                wait(delta);
            } else {
                wait();
            }
            if (!cancelado && until <= System.currentTimeMillis()) {
                fazAlgo();
            }
        } catch (InterruptedException ex) { }
    }
}

Reading the documentation of Thread, I saw that a new method has been made available: onSpinWait, where an API note says that the call of this method should be placed where loop is used (in the context of Thread), but also says that not using it is correct.

That being said, I would like to know what is the need of using this method and whether it is really necessary or not to use it.

1 answer

2


That method Thread.onSpinWait() is for a thread to tell the JVM that it is in a busy waiting process. That is, it signals that the thread is inside a loop waiting for something to happen, and although it is not sleeping or blocked, it is also not making progress in the work it intends to do. Thus, this method informs the JVM that it can perform some kind of performance optimization in this situation.

The example of Javadoc exemplifies the case well:

class EventHandler {
    volatile boolean eventNotificationNotReceived;
    void waitForEventAndHandleIt() {
        while ( eventNotificationNotReceived ) {
            java.lang.Thread.onSpinWait();
        }
        readAndProcessEvent();
    }

    void readAndProcessEvent() {
        // Read event from some source and process it
         . . .
    }
}

Note that in this code it is inside a while waiting for the variable to change to false. The variable has the modifier volatile, then it is possible that its value changes suddenly when changed by another thread. However, as long as it remains as true, the thread will be consuming CPU unnecessarily, that is, at the same time that the thread is busy (because it is consuming CPU, not sleeping and not blocked), it is also waiting for something to happen - this is the busy waiting.

This method is something of a very punctual and casuistic purpose, being used for very specific Engineering and micro-optimizations. It is something that is very easy to be used improperly and very difficult to use properly. In addition, the JVM in question can ignore this method completely, and implement it simply by doing nothing.

I see a certain relationship with System.gc() and with the Thread.yield(). The three serve to warn the JVM that a certain optimization may be appropriate, but the JVM can choose to ignore this completely and it is also something that should have no impact on the behavior or functionality of the program, only on performance. And also the three have in common a strong propensity to be misused.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.