Skip to content

Commit

Permalink
Started example on suspend execution.
Browse files Browse the repository at this point in the history
  • Loading branch information
chumer committed Jul 9, 2017
1 parent 051bcf5 commit 0e0f5be
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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.Context;
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 SuspendExecution {

public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService service = Executors.newFixedThreadPool(1);
Engine engine = Engine.create();
Context context = engine.getLanguage("js").createContext();
// we submit an endless script to the executor
Future<Integer> future = service.submit(() -> context.eval("while(true)").asInt());

// do work while JavaScript executes

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

engine.close();
}

}

0 comments on commit 0e0f5be

Please sign in to comment.