ScopeTest.java 文件源码

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

项目:openjdk-jdk10 作者:
/**
 * Test multi-threaded access to global getters and setters for shared script classes with multiple globals.
 */
@Test
public static void getterSetter2Test() 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);
    final String sharedScript = "accessor2";

    e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), origContext);
    assertEquals(e.eval("accessor2 = 1;"), 1);
    assertEquals(e.eval(sharedScript), 1);

    e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), newCtxt);
    assertEquals(e.eval("accessor2 = 2;", newCtxt), 2);
    assertEquals(e.eval(sharedScript, newCtxt), 2);


    final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, 1, 1000));
    final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, 2, 1000));

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

    assertEquals(e.eval(sharedScript), 1);
    assertEquals(e.eval(sharedScript, newCtxt), 2);
    assertEquals(e.eval("x"), 1);
    assertEquals(e.eval("x", newCtxt), 2);
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号