mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-06-07 00:16:32 +00:00
perf[time]: volatile reduces the cache hit ratio of the CPU
This commit is contained in:
@@ -68,8 +68,6 @@ public abstract class SchedulerBus {
|
||||
logger.error("scheduler triggers an error.", e);
|
||||
}
|
||||
}, 0, TimeUtils.MILLIS_PER_SECOND, TimeUnit.MILLISECONDS);
|
||||
|
||||
executor.scheduleAtFixedRate(TimeUtils::currentTimeMillis, 0, 20, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public static class SchedulerThreadFactory implements ThreadFactory {
|
||||
|
||||
@@ -83,7 +83,8 @@ public abstract class TimeUtils {
|
||||
SchedulerBus.refreshMinTriggerTimestamp();
|
||||
}
|
||||
|
||||
private static volatile long timestamp = System.currentTimeMillis();
|
||||
// volatile reduces the cache hit ratio of the CPU
|
||||
private static long timestamp = System.currentTimeMillis();
|
||||
|
||||
/**
|
||||
* 获取精确的时间戳
|
||||
@@ -94,9 +95,9 @@ public abstract class TimeUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最多只有20ms延迟的粗略时间戳,适用于对时间精度要求不高的场景,最多只有20ms误差
|
||||
* CN:获取最多只有一秒延迟的粗略时间戳,适用于对时间精度要求不高的场景,比System.currentTimeMillis()的性能高10倍
|
||||
* <p>
|
||||
* 比System.currentTimeMillis()的性能高10倍
|
||||
* EN:Obtain a coarse timestamp with a delay of up to one second, which is suitable for scenarios that do not require high time accuracy
|
||||
*/
|
||||
public static long now() {
|
||||
return timestamp;
|
||||
|
||||
Reference in New Issue
Block a user