From 304803a47404ce172c4301bb5e3f01478e1e71ee Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Thu, 27 May 2021 01:10:24 -0600 Subject: [PATCH] Use monotonic clocks for SystemMonitor. --- .../java/org/jpos/q2/qbean/SystemMonitor.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/jpos/src/main/java/org/jpos/q2/qbean/SystemMonitor.java b/jpos/src/main/java/org/jpos/q2/qbean/SystemMonitor.java index 606912d9f9..9f1de72f67 100644 --- a/jpos/src/main/java/org/jpos/q2/qbean/SystemMonitor.java +++ b/jpos/src/main/java/org/jpos/q2/qbean/SystemMonitor.java @@ -36,6 +36,7 @@ import java.net.InetAddress; import java.nio.charset.Charset; import java.security.NoSuchAlgorithmException; +import java.time.Duration; import java.time.Instant; import java.time.ZoneId; import java.time.format.TextStyle; @@ -43,7 +44,6 @@ import java.time.zone.ZoneOffsetTransitionRule; import java.util.List; import java.util.Locale; -import java.util.Map; /** * Periodically dumps Thread and memory usage @@ -55,8 +55,8 @@ public class SystemMonitor extends QBeanSupport implements Runnable, SystemMonitorMBean, Loggeable { - private long sleepTime = 60 * 60 * 1000; - private long delay = 0; + private Duration sleepTime = Duration.ofHours(1); + private Duration delay = Duration.ZERO; private boolean detailRequired = false; private Thread me = null; private static final int MB = 1024*1024; @@ -82,14 +82,14 @@ public void stopService() { } public synchronized void setSleepTime(long sleepTime) { - this.sleepTime = sleepTime; + this.sleepTime = Duration.ofMillis(sleepTime); setModified(true); if (me != null) me.interrupt(); } public synchronized long getSleepTime() { - return sleepTime; + return sleepTime.toMillis(); } public synchronized void setDetailRequired(boolean detail) { @@ -125,9 +125,9 @@ public void run() { log.info(this); frozenDump = null; try { - long expected = System.currentTimeMillis() + sleepTime; - Thread.sleep(sleepTime); - delay = System.currentTimeMillis() - expected; + Duration expected = Duration.ofNanos(System.nanoTime()).plus(sleepTime); + Thread.sleep(sleepTime.toMillis()); + delay = Duration.ofNanos(System.nanoTime()).minus(expected); } catch (InterruptedException ignored) { } } @@ -230,7 +230,7 @@ private String generateFrozenDump(String indent) { Instant in = tran.getInstant(); p.printf("%s transition: %s (%s)%n", indent, in, in.atZone(zi)); } - p.printf("%s clock: %d %s%n", indent, System.currentTimeMillis() / 1000L, instant); + p.printf("%s clock: %d %s%n", indent, Instant.now().getEpochSecond(), instant); if (hasSecurityManager()) p.printf("%s sec-manager: %s%n", indent, getSecurityManager()); p.printf("%s thread count: %d%n", indent, mxBean.getThreadCount());