Skip to content

Instantly share code, notes, and snippets.

View eleinadani's full-sized avatar

Daniele Bonetta eleinadani

  • VU Amsterdam
View GitHub Profile
// 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();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
json.execute(42,43).asString();
}
}
});
thread.start();
while (true) {
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();
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() {
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();
}
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
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);
});
let w = new Worker(`
const { parentPort } = require('worker_threads');
parentPort.on('message', (m) => {
const x = m.random.nextInt(100);
const y = m.random.nextInt(100);
var Point = Java.type('java.awt.Point');
parentPort.postMessage(new Point(x,y));
});
`, {eval:true});
function JavaToJSNotifier {
this.queue = new java.util.concurrent.LinkedBlockingDeque();
this.worker = new Worker(`
const { workerData, parentPort } = require('worker_threads');
while (true) {
// block the worker waiting for the next notification from Java
var data = workerData.queue.take();
// notify the main event loop that we got new data
parentPort.postMessage(data);
}`,
public class AwesomeClass {
// a concurrent queue shared with Node
private final Queue<Object> queue;
public AwesomeClass(Queue<Object> queue) {
this.queue = queue;
}
public void start() {
Thread thread = new Thread(new Runnable() {