Skip to content

Commit

Permalink
Add example for parallel eval.
Browse files Browse the repository at this point in the history
  • Loading branch information
chumer committed Jul 9, 2017
1 parent 7408075 commit 051bcf5
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.graalvm.polyglot.examples;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.graalvm.polyglot.Engine;

/**
* Example shows how scripts can be executed in parallel. Please note that contexts are single
* threaded by design. It is allowed to use a context on more than one thread, but not at the same
* time.
*/
public class ParallelEval {

public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService service = Executors.newFixedThreadPool(1);
Engine engine = Engine.create();
Future<Integer> future = service.submit(() -> engine.getLanguage("js").createContext().eval("21 + 21").asInt());

// do work while JavaScript executes

int result = future.get();
assert 42 == result;

engine.close();
}

}

0 comments on commit 051bcf5

Please sign in to comment.