If timeout is zero or negative there is no need to make a costly call to sample the time.
@@ -29,10 +29,7 @@ public class ConfigSetSubscription<T extends ConfigInstance> extends ConfigSubsc
setGeneration(0L);
}
- @Override
- public boolean nextConfig(long timeout) {
- long end = System.currentTimeMillis() + timeout;
- do {
+ private boolean hasConfigChanged() {
T myInstance = getNewInstance();
ConfigState<T> configState = getConfigState();
// User forced reload
@@ -44,8 +41,19 @@ public class ConfigSetSubscription<T extends ConfigInstance> extends ConfigSubsc
setConfigIncGen(myInstance);
return true;
}
+ return false;
+ }
+
+ @Override
+ public boolean nextConfig(long timeout) {
+ if (hasConfigChanged()) return true;
+ if (timeout <= 0) return false;
+
+ long end = System.nanoTime() + timeout * 1_000_000;
+ do {
sleep();
- } while (System.currentTimeMillis() < end);
+ if (hasConfigChanged()) return true;
+ } while (System.nanoTime() < end);
return false;
}
