java忙,在Java中什么是快速,等待通知或忙等待?

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:

0S0Tv.png

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));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值