Skip to content

Instantly share code, notes, and snippets.

@eleinadani
Last active February 5, 2019 16:15
Show Gist options
  • Save eleinadani/e8adb09e1ca3eaaef33d3b8403fd8278 to your computer and use it in GitHub Desktop.
Save eleinadani/e8adb09e1ca3eaaef33d3b8403fd8278 to your computer and use it in GitHub Desktop.
Context c1 = Context.create("js");
Context c2 = Context.create("js");
// create and share a thread-safe counter
AtomicInteger counter = new AtomicInteger(0);
c1.getBindings("js").putMember("counter", counter);
c2.getBindings("js").putMember("counter", counter);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (counter.get() < 1000) {
c1.eval("js", "counter.incrementAndGet()");
}
}
});
thread.start();
while (counter.get() < 1000) {
c2.eval("js", "counter.incrementAndGet()");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment