forked from dropwizard/metrics
/
Clock.java
54 lines (46 loc) · 1.24 KB
/
Clock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.yammer.metrics.core;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
/**
* An abstraction for how time passes. It is passed to {@link TimerMetric} to
* track timing.
*/
public abstract class Clock {
/**
* Gets the current time tick
*
* @return time tick in nanoseconds
*/
public abstract long tick();
/**
* Gets the current time in milliseconds
*
* @return time in milliseconds
*/
public long time() {
return System.currentTimeMillis();
}
/**
* The default clock to use.
*/
public static final Clock DEFAULT = new UserTime();
/**
* Default implementation, uses {@link System#nanoTime()}.
*/
public static class UserTime extends Clock {
@Override
public long tick() {
return System.nanoTime();
}
}
/**
* Another implementation, uses {@link ThreadMXBean#getCurrentThreadCpuTime()}
*/
public static class CpuTime extends Clock {
private static ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
@Override
public long tick() {
return threadMxBean.getCurrentThreadCpuTime();
}
}
}