Javaの基本的な同期/待機に実装された典型的な条件変数があります:
ConcurrentHashMap incompleted = ...;
// the notifier
incompleted.remove(key);
synchronized (this) {
if (incompleted.isEmpty()) {
notifyAll();
}
}
// the waiter
synchronized (this) { // one this object for each request
while (!incompleted.isEmpty()) {
wait(10000L); // this is exact time out pass in
}
// done and exit
}
これらのコードは非常に典型的で機能します。ただし、多くの (たとえば 100 の) 同時要求でテストすると、CPU 負荷は約 80% であり、プロファイラーはアプリが wait() メソッドで費やした時間の 80% を報告します。通常、CPU が高いのは、アプリ コードでビジー状態で待機していることが原因である可能性があります。しかし、wait() 自体が常に時間を費やすのはなぜでしょうか? ありがとう
ホストは、Oracle JVM 1.6 を実行する VMware ホストです。