forked from oracle/graal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started example on suspend execution.
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
...src/org.graalvm.polyglot.examples/src/org/graalvm/polyglot/examples/SuspendExecution.java
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
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(); | ||
} | ||
|
||
} |