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
// Create the JS runtime (aka, "Context") | |
Context cx = Context.create("js"); | |
Value json = cx.eval("js", "(function(x,y) { return JSON.stringify({x:x,y:y}); })"); | |
// Use the function | |
json.execute(42,43).asString(); |
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
Thread thread = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
while (true) { | |
json.execute(42,43).asString(); | |
} | |
} | |
}); | |
thread.start(); | |
while (true) { |
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
const Thread = Java.type('java.lang.Thread') | |
// `function run()` is converted to a Java Runnable instance by GraalVM | |
const t = new Thread(function run() { | |
// this will never be executed, as an exception will be thrown | |
console.log('hello from another thread!') | |
}); | |
// creating a thread does not imply concurrent access to a shared JS object. | |
// starting the thread, however, will result in a concurrent access to the | |
// shared Runnable object (that is, the JavaScript 'run' function above). | |
t.start(); |
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
Context c1 = Context.create("js"); | |
Context c2 = Context.create("js"); | |
String jsCode = "(function(x,y) { return JSON.stringify({x:x,y:y}); })"; | |
Value json1 = c1.eval("js", jsCode); | |
Value json2 = c2.eval("js", jsCode); | |
Thread thread = new Thread(new Runnable() { | |
@Override | |
public void run() { |
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
Context cx = Context.create("js"); | |
Value jsCode = cx.eval("js", "(function(x,y) { return JSON.stringify({x:x,y:y}); })"); | |
Thread thread = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
while (true) { | |
synchronized (cx) { | |
jsCode.execute(42,43).asString(); | |
} |
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
let w = new Worker(` | |
const JavaClass = Java.type('my.very.important.JavaClass'); | |
const { parentPort } = require('worker_threads'); | |
parentPort.postMessage(JavaClass.someVeryLongCall()); | |
`, {eval:true}); | |
w.on('message', (m) => { | |
console.log('Got data from Java, via worker thread:' + m); | |
}); |
OlderNewer