long current = System.currentTimeMillis(); this.cachedTime = new AtomicReference<CachedTime>(new CachedTime(current, fastDateFormat.format(current))); }
public String format(finallong timestampMillis){ CachedTime cached = cachedTime.get();
long timestamp = onSecond ? timestampMillis / 1000 : timestampMillis;
if (timestamp != cached.timestamp) { final CachedTime newCachedTime = new CachedTime(timestamp, fastDateFormat.format(timestampMillis)); if (cachedTime.compareAndSet(cached, newCachedTime)) { cached = newCachedTime; } else { cached = cachedTime.get(); } }
return cached.formatted; }
staticfinalclassCachedTime{ publiclong timestamp; public String formatted;