Created
July 18, 2013 05:43
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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