Last active
November 26, 2017 16:16
-
-
Save qsLI/a42cea0411b2cff131f34d82d030115b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.air.jmh; | |
import java.sql.Timestamp; | |
import java.util.Date; | |
import java.util.Locale; | |
import java.util.TimeZone; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ScheduledExecutorService; | |
import java.util.concurrent.TimeUnit; | |
import org.apache.commons.lang3.time.FastDateFormat; | |
/** | |
* 缓存系统时间,避免每次调用System.currentTimeMillis()消耗系统资源。适用于高并发、对时间精度要求不高的场景 | |
*/ | |
public class SystemTimer { | |
private final static ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); | |
/** | |
* 更新间隔 | |
*/ | |
private static final long interval = Long.parseLong(System.getProperty("notify.systimer.interval", "1000")); | |
/** | |
* 时间格式 | |
*/ | |
private static final FastDateFormat formatyyyyMMddHHmmss = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss", | |
TimeZone.getDefault(), Locale.getDefault()); | |
/** | |
* 时间格式 | |
*/ | |
private static final FastDateFormat formatyyyyMMdd = FastDateFormat.getInstance("yyyy-MM-dd", | |
TimeZone.getDefault(), Locale.getDefault()); | |
/** | |
* 时间 | |
*/ | |
private static volatile long time = System.currentTimeMillis(); | |
private static volatile String timeyyyyMMddHHmmss = formatyyyyMMddHHmmss.format(new Date()); | |
private static volatile String timeyyyyMMdd = formatyyyyMMdd.format(new Date()); | |
public SystemTimer() { | |
} | |
private static class TimerFormatTicker implements Runnable { | |
@Override | |
public void run() { | |
time = System.currentTimeMillis(); | |
timeyyyyMMddHHmmss = new Timestamp(time).toString().substring(0, 19); | |
timeyyyyMMdd = timeyyyyMMddHHmmss.substring(0, 10);// 通过对timeyyyyMMddHHmmss进行substring进行优化,减少运算 | |
} | |
} | |
public static long currentTimeMillis() { | |
return System.currentTimeMillis(); | |
} | |
/** | |
* 获取 | |
* | |
* @return | |
*/ | |
public static String getTimeyyyyMMddHHmmss() { | |
return timeyyyyMMddHHmmss; | |
} | |
public static String getTimeyyyyMMdd() { | |
return timeyyyyMMdd; | |
} | |
static { | |
executor.scheduleAtFixedRate(new TimerFormatTicker(), interval, interval, TimeUnit.MILLISECONDS); | |
Runtime.getRuntime().addShutdownHook(new Thread() { | |
@Override | |
public void run() { | |
executor.shutdown(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment