introduced MillisecondsTimePoint.approximateNow() and used where now() is too expensive; re-typed now() to TimePoint

This commit is contained in:
Axel Uhl committed 2015-07-27 22:48:09 +02:00
1 parent 27338ad216
commit 78ff26a17e
26 files changed
+88 -66

No files matched your search

@@ -1,6 +1,10 @@
package com.sap.sse.common.impl;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import com.sap.sse.common.TimePoint;
public class MillisecondsTimePoint extends AbstractTimePoint {
@@ -8,7 +12,33 @@ public class MillisecondsTimePoint extends AbstractTimePoint {
private long millis;
private Date date;
public static MillisecondsTimePoint now() {
private static TimePoint timePointNoOlderThanOneSecond;
static {
timePointNoOlderThanOneSecond = MillisecondsTimePoint.now();
new Timer("LockUtil timestamp generator", /* isDaemon */ true).schedule(
new TimerTask() {
@Override
public void run() {
timePointNoOlderThanOneSecond = MillisecondsTimePoint.now();
}
}, /* delay 0 means immediate execution */ 0l, /* period 1s */ 1000l);
}
/**
* A time point that is approximately the current time, up to one second late. Obtaining a time point
* this way is much less expensive than using {@link #now()} which really generates a new time point
* upon each invocation and as such does not qualify for high-frequency operations, particularly when
* the timer is used for some human time-based timeout of several seconds.
*/
public static TimePoint approximateNow() {
return timePointNoOlderThanOneSecond;
}
/**
* A time point as exact as {@link System#currentTimeMillis()}
*/
public static TimePoint now() {
return new MillisecondsTimePoint(System.currentTimeMillis());
}