Javascript 如何在Nashorn JS脚本中调用Thread.sleep
我尝试在Nashorn中的各种情况下使用Thread.sleep,但它要么同时执行,要么抛出异常。如何在脚本中使用Thread.sleep这段代码似乎对我很有用Javascript 如何在Nashorn JS脚本中调用Thread.sleep,javascript,java,nashorn,Javascript,Java,Nashorn,我尝试在Nashorn中的各种情况下使用Thread.sleep,但它要么同时执行,要么抛出异常。如何在脚本中使用Thread.sleep这段代码似乎对我很有用 ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine se = sem.getEngineByExtension("js"); int time_in_ms = 3000; se.put("ms", time_in_ms);
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine se = sem.getEngineByExtension("js");
int time_in_ms = 3000;
se.put("ms", time_in_ms);
Instant before = Instant.now();
se.eval("java.lang.Thread.sleep(ms)");
Instant after = Instant.now();
System.out.printf("Expected: %dms Actual %dms%n", time_in_ms, Duration.between(before, after).toMillis());
我可以以毫秒为单位改变时间,得到不同的值,例如
Expected: 3000ms Actual 3195ms
显然,由于评估javascript的开销,它们是不同的
我在Windows下使用JDK1.8和JDK11进行了尝试。能否显示调用Java方法的JS代码?能否确认在JavaScript中调用完全限定的方法Java.lang.Thread.sleepms,而不仅仅是Thread.sleepms