Optimize method io.netty.util.concurrent.DefaultPromise.await0(...) (#11504)
Motivation: For the code pattern of `Object.wait(...)` in `io.netty.util.concurrent.DefaultPromise.await0(...)`, it's better to follow the recommended code pattern according to [Object.wait(...)'s doc](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#wait()): ``` synchronized (obj) { while (<condition does not hold>) obj.wait(); ... // Perform action appropriate to condition } ``` Modification: Change the `Object.wait(...)`'s code pattern in `io.netty.util.concurrent.DefaultPromise.await0(...)`. Result: The `Object.wait(...)`'s code pattern in `io.netty.util.concurrent.DefaultPromise.await0(...)` meets the Java doc.
This commit is contained in:
parent
eb99f6c74a
commit
8f2fe8659b
@ -666,15 +666,14 @@ public class DefaultPromise<V> extends AbstractFuture<V> implements Promise<V> {
|
||||
|
||||
checkDeadLock();
|
||||
|
||||
long startTime = System.nanoTime();
|
||||
long waitTime = timeoutNanos;
|
||||
// Start counting time from here instead of the first line of this method,
|
||||
// to avoid/postpone performance cost of System.nanoTime().
|
||||
final long startTime = System.nanoTime();
|
||||
synchronized (this) {
|
||||
boolean interrupted = false;
|
||||
try {
|
||||
for (;;) {
|
||||
synchronized (this) {
|
||||
if (isDone()) {
|
||||
return true;
|
||||
}
|
||||
long waitTime = timeoutNanos;
|
||||
while (!isDone() && waitTime > 0) {
|
||||
incWaiters();
|
||||
try {
|
||||
wait(waitTime / 1000000, (int) (waitTime % 1000000));
|
||||
@ -687,22 +686,22 @@ public class DefaultPromise<V> extends AbstractFuture<V> implements Promise<V> {
|
||||
} finally {
|
||||
decWaiters();
|
||||
}
|
||||
}
|
||||
// Check isDone() in advance, try to avoid calculating the elapsed time later.
|
||||
if (isDone()) {
|
||||
return true;
|
||||
} else {
|
||||
}
|
||||
// Calculate the elapsed time here instead of in the while condition,
|
||||
// try to avoid performance cost of System.nanoTime() in the first loop of while.
|
||||
waitTime = timeoutNanos - (System.nanoTime() - startTime);
|
||||
if (waitTime <= 0) {
|
||||
}
|
||||
return isDone();
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (interrupted) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify all progressive listeners.
|
||||
|
Loading…
Reference in New Issue
Block a user