Skip to content

Commit

Permalink
feat: use tick thread to retrieve current time
Browse files Browse the repository at this point in the history
  • Loading branch information
killme2008 committed Jun 8, 2024
1 parent a147bc6 commit 63990cf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.IOException;
import java.math.MathContext;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.AviatorEvaluatorInstance;
import com.googlecode.aviator.Options;
Expand Down Expand Up @@ -31,7 +30,8 @@
*/
public final class RuntimeUtils {

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

private RuntimeUtils() {

Expand Down
36 changes: 34 additions & 2 deletions src/main/java/com/googlecode/aviator/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import java.security.MessageDigest;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import com.googlecode.aviator.runtime.RuntimeUtils;
import com.googlecode.aviator.runtime.type.AviatorBigInt;
import com.googlecode.aviator.runtime.type.AviatorDecimal;
Expand All @@ -22,15 +26,43 @@
*
*/
public class Utils {
private static final String CURRENT_VERSION = "5.0.0";
private static final String CURRENT_VERSION = "5.4.2";

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

private Utils() {

}

private static class StaticHolder {
static ScheduledExecutorService tickService =
Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {

@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setDaemon(true);
thread.setName("aviatorscript-tick-thread");
return thread;
}
});

static volatile long nowNs = System.nanoTime();

static {
tickService.scheduleAtFixedRate(new Runnable() {

@Override
public void run() {
nowNs = System.nanoTime();
}
}, 0, TICK_INTERVAL_MS, TimeUnit.MILLISECONDS);
}
}

public static long currentTimeNanos() {
return System.nanoTime();
return StaticHolder.nowNs;
}

private static final ThreadLocal<MessageDigest> MESSAGE_DIGEST_LOCAL =
Expand Down

0 comments on commit 63990cf

Please sign in to comment.