ScopeTest.java 文件源码

java
阅读 21 收藏 0 点赞 0 评论 0

项目:openjdk-jdk10 作者:
/**
 * Test multi-threaded scope function invocation for shared script classes with multiple globals.
 */
@Test
public static void multiThreadedFunctionTest() throws ScriptException, InterruptedException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Bindings b = e.createBindings();
    final ScriptContext origContext = e.getContext();
    final ScriptContext newCtxt = new SimpleScriptContext();
    newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);

    e.eval(new URLReader(ScopeTest.class.getResource("resources/func.js")), origContext);
    assertEquals(origContext.getAttribute("scopeVar"), 1);
    assertEquals(e.eval("scopeTest()"), 1);

    e.eval(new URLReader(ScopeTest.class.getResource("resources/func.js")), newCtxt);
    assertEquals(newCtxt.getAttribute("scopeVar"), 1);
    assertEquals(e.eval("scopeTest();", newCtxt), 1);

    assertEquals(e.eval("scopeVar = 3;", newCtxt), 3);
    assertEquals(newCtxt.getAttribute("scopeVar"), 3);


    final Thread t1 = new Thread(new ScriptRunner(e, origContext, "scopeTest()", 1, 1000));
    final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, "scopeTest()", 3, 1000));

    t1.start();
    t2.start();
    t1.join();
    t2.join();

}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号