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 know if one is ready to sacrifice cpu cycles, then will busy wait be faster or wait-notify ?
I assume that wait-notify will involve an intrinsic lock over synchronized object and also signal might come from kernel to wake-up the thread, making this approach much slower than a busy-wait, where one could just check a condition continuously till it is satisfied. As soon as this condition is satisfied (e.g. boolean value == true), thread could come out of busy wait. As per my understanding, I feel busy-wait should be faster.
I would appreciate if others can share their thoughts and correct me if my argument is wrong.
解决方案
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:
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));
}
}