Skip to content

Instantly share code, notes, and snippets.

@tobsch
Created July 18, 2013 05:43
Show Gist options
  • Save tobsch/6026942 to your computer and use it in GitHub Desktop.
Save tobsch/6026942 to your computer and use it in GitHub Desktop.
import javax.script.*;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import jdk.nashorn.api.scripting.NashornScriptEngine;
public class Tester {
static class NashornThread extends Thread {
private Bindings runner;
NashornThread(String name, Bindings runner){
super(name);
this.runner = runner;
}
public void run() {
try {
for (long i = 0; i < new Long("10000000000"); i++) {
engine.invokeMethod(runner, "run", i);
//System.out.println(getName() + ":" + );
}
}catch(Exception e ){
System.out.println("Error: " + e);
}
System.out.println("Done with " + getName());
}
}
private static final NashornScriptEngineFactory engineFactory = new NashornScriptEngineFactory();
private static final NashornScriptEngine engine = (NashornScriptEngine) engineFactory.getScriptEngine();
public static void main(String[] args) throws Exception {
ScriptEngine e = new ScriptEngineManager().getEngineByName("nashorn");
Bindings runner = (Bindings) engine.compile("runner = { run: function(foo){ return foo * 2 } }; runner").eval();
Tester foo = new Tester();
NashornThread threads[] = new NashornThread[4];
for (int i = 0; i < 4; i++) {
threads[i] = new NashornThread(String.valueOf(i), runner);
threads[i].start();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment