看到解释比较认可的讲解链接:https://www.jianshu.com/p/aeb391e4edb0
实际的使用:
public static ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3);
private static LocalCacheService cacheService = new LocalCacheService();
// Every two hours refresh
public void triggerFresh() {
String pUmpKey = appConfig.getUmp_key_prefix() + ".LocalCacheRefreshSchedule.triggerFresh";
CallerInfo info = Profiler.registerInfo(pUmpKey, appConfig.getAppName(), false, true);
try {
executorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
cacheService.constructBackupDataByDoudiPin();
cacheService.writeToLocalFile();
}
}, 0, 3, TimeUnit.HOURS);
} catch (Exception e) {
log.error("LocalCacheRefreshSchedule triggerFresh Error:{}", e);
Profiler.functionError(info);
} finally {
Profiler.registerInfoEnd(info);
}
}