Skip to content

Commit 63990cf

Browse files
committed
feat: use tick thread to retrieve current time
1 parent a147bc6 commit 63990cf

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

src/main/java/com/googlecode/aviator/runtime/RuntimeUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.IOException;
44
import java.math.MathContext;
55
import java.util.Map;
6-
import java.util.concurrent.TimeUnit;
76
import com.googlecode.aviator.AviatorEvaluator;
87
import com.googlecode.aviator.AviatorEvaluatorInstance;
98
import com.googlecode.aviator.Options;
@@ -31,7 +30,8 @@
3130
*/
3231
public final class RuntimeUtils {
3332

34-
private static final int CHECKPOINTS = 3000;
33+
private static final int CHECKPOINTS =
34+
Integer.parseInt(System.getProperty("aviator.execution.timeout.checkpoints", "2000"));
3535

3636
private RuntimeUtils() {
3737

src/main/java/com/googlecode/aviator/utils/Utils.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import java.security.MessageDigest;
99
import java.util.Map;
1010
import java.util.Properties;
11+
import java.util.concurrent.Executors;
12+
import java.util.concurrent.ScheduledExecutorService;
13+
import java.util.concurrent.ThreadFactory;
14+
import java.util.concurrent.TimeUnit;
1115
import com.googlecode.aviator.runtime.RuntimeUtils;
1216
import com.googlecode.aviator.runtime.type.AviatorBigInt;
1317
import com.googlecode.aviator.runtime.type.AviatorDecimal;
@@ -22,15 +26,43 @@
2226
*
2327
*/
2428
public class Utils {
25-
private static final String CURRENT_VERSION = "5.0.0";
29+
private static final String CURRENT_VERSION = "5.4.2";
2630

31+
private static final int TICK_INTERVAL_MS =
32+
Integer.parseInt(System.getProperty("aviator.tick.interval_ms", "1"));
2733

2834
private Utils() {
2935

3036
}
3137

38+
private static class StaticHolder {
39+
static ScheduledExecutorService tickService =
40+
Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
41+
42+
@Override
43+
public Thread newThread(Runnable r) {
44+
Thread thread = new Thread(r);
45+
thread.setDaemon(true);
46+
thread.setName("aviatorscript-tick-thread");
47+
return thread;
48+
}
49+
});
50+
51+
static volatile long nowNs = System.nanoTime();
52+
53+
static {
54+
tickService.scheduleAtFixedRate(new Runnable() {
55+
56+
@Override
57+
public void run() {
58+
nowNs = System.nanoTime();
59+
}
60+
}, 0, TICK_INTERVAL_MS, TimeUnit.MILLISECONDS);
61+
}
62+
}
63+
3264
public static long currentTimeNanos() {
33-
return System.nanoTime();
65+
return StaticHolder.nowNs;
3466
}
3567

3668
private static final ThreadLocal<MessageDigest> MESSAGE_DIGEST_LOCAL =

0 commit comments

Comments
 (0)