What is fast, wait notify or busy wait in Java?

前端 未结 4 1798
一个人的身影
一个人的身影 2021-01-05 07:28

I understand that use of busy-wait is not a good programming practice and preferably synchronized object (wait-notify) should be used whenever possible. But I would like to

4条回答
  •  有刺的猬
    2021-01-05 07:54

    Experimentation suggests that you will see the flag sooner if you busy wait than if you wait and notify (on my hardware, anyway). (Details below.) The difference is very very very very very small and so this would only be applicable to very rare apps. Stock trading apps, for instance, where companies are after any advantage they can get (vying to locate their servers as near the exchange as possible to get microsecond improvements in their network feeds from the exchange and such) might consider the difference worth it. I can imagine some science applications as well.

    In the vast majority of apps, the difference will be in effect no difference at all.

    But what happens to the CPU is, of course, that one of the cores hard-pegs:

    hardpeg

    That's bad in terms of impacting other processes on the box and in terms of power consumption in the data center.

    So: Use with extreme reluctance, only in situations where it really matters.


    Data (very small sample, but the code follows):

    Busy Wait:       10631  12350  15278
    Wait and Notify: 87299 120964 107204
    Delta:           76668 108614  91926
    

    Times are in nanoseconds. Billionths of a second. The average delta above is 92403ns (0.092402667 milliseconds, 0.000092403 seconds).

    BusyWait.java:

    public class BusyWait {
    
        private static class Shared {
            public long setAt;
            public long seenAt;
            public volatile boolean flag = false;
        }
    
        public static void main(String[] args) {
            final Shared shared = new Shared();
            Thread notifier = new Thread(new Runnable() {
                public void run() {
                    System.out.println("Running");
                    try {
                        Thread.sleep(500);
                        System.out.println("Setting flag");
                        shared.setAt = System.nanoTime();
                        shared.flag = true;
                    }
                    catch (Exception e) {
                    }
                }
            });
            notifier.start();
            while (!shared.flag) {
            }
            shared.seenAt = System.nanoTime();
            System.out.println("Delay between set and seen: " + (shared.seenAt - shared.setAt));
        }
    }
    

    WaitAndNotify.java:

    public class WaitAndNotify {
    
        private static class Shared {
            public long setAt;
            public long seenAt;
            public boolean flag = false;
        }
    
        public static void main(String[] args) {
            (new WaitAndNotify()).test();
        }
        private void test() {
            final Shared shared = new Shared();
            final WaitAndNotify instance = this;
            Thread notifier = new Thread(new Runnable() {
                public void run() {
                    System.out.println("Running");
                    try {
                        Thread.sleep(500);
                        System.out.println("Setting flag");
                        shared.setAt = System.nanoTime();
                        shared.flag = true;
                        synchronized (instance) {
                            instance.notify();
                        }
                    }
                    catch (Exception e) {
                    }
                }
            });
            notifier.start();
            while (!shared.flag) {
                try {
                    synchronized (this) {
                        wait();
                    }
                }
                catch (InterruptedException ie) {
                }
            }
            shared.seenAt = System.nanoTime();
            System.out.println("Delay between set and seen: " + (shared.seenAt - shared.setAt));
        }
    }
    

提交回复
热议问题