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.
- Loading branch information
Showing
8 changed files
with
124 additions
and
76 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
.../src/org.graalvm.polyglot.examples/src/org/graalvm/polyglot/examples/CancelExecution.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,49 @@ | ||
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; | ||
import org.graalvm.polyglot.PolyglotException; | ||
import org.graalvm.polyglot.Value; | ||
|
||
/** | ||
* This example shows how harmful long running scripts can be cancelled. Cancelling the current | ||
* execution will render the context inconsistent and therefore unusable. | ||
*/ | ||
public class CancelExecution { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
ExecutorService service = Executors.newFixedThreadPool(1); | ||
Engine engine = Engine.create(); | ||
Context context = engine.getLanguage("js").createContext(); | ||
|
||
// we submit a harmful infinite script to the executor | ||
Future<Value> future = service.submit(() -> context.eval("while(true)")); | ||
|
||
// wait some time to let the execution complete. | ||
Thread.sleep(1000); | ||
|
||
/* | ||
* closes the context and cancels the running execution. This can be done on any parallel | ||
* thread. Alternatively Engine.close(true) can be used to close all running contexts of an | ||
* engine. | ||
*/ | ||
context.close(true); | ||
|
||
try { | ||
future.get(); | ||
} catch (ExecutionException e) { | ||
PolyglotException polyglotException = (PolyglotException) e.getCause(); | ||
/* | ||
* After the execution got cancelled the executing thread stops by throwning a | ||
* PolyglotException with the cancelled flag set. | ||
*/ | ||
assert polyglotException.isCancelled(); | ||
} | ||
} | ||
|
||
} |
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
33 changes: 0 additions & 33 deletions
33
...src/org.graalvm.polyglot.examples/src/org/graalvm/polyglot/examples/SuspendExecution.java
This file was deleted.
Oops, something went wrong.
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
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
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
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
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