blob: 5aa4990a86a7adbf20b3e6d2c4329cbd6535cbf7 (
plain) (
blame)
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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.concurrent;
import java.util.concurrent.TimeUnit;
/**
* This is an implementation of {@link Timer} that is backed by an actual system timer.
*
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
*/
public enum SystemTimer implements Timer {
INSTANCE;
private volatile long millis;
private SystemTimer() {
millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
Thread thread = new Thread() {
@Override
public void run() {
while (true) {
millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
try {
Thread.sleep(1);
} catch (InterruptedException e) {
break;
}
}
}
};
thread.setDaemon(true);
thread.start();
}
@Override
public long milliTime() {
return millis;
}
}
|