- GraalVM for JDK 23 (Latest)
- GraalVM for JDK 24 (Early Access)
- GraalVM for JDK 21
- GraalVM for JDK 17
- Archives
- Dev Build
- GraalJS
- Node.js Runtime
- Java Interoperability
- GraalJS Compatibility
- Using JavaScript Modules and Packages
- Multithreading
- Migration Guide from Nashorn to GraalJS
- Migration Guide from Rhino to GraalJS
- Differences Between Node.js and Java Embeddings
- Options
- Run GraalJS on a Stock JDK
- ScriptEngine Implementation
- FAQ
Multithreading
Running JavaScript on GraalVM supports multithreading.
Depending on the usage scenario, threads can be used to execute parallel JavaScript code using multiple Context
objects, or multiple Worker threads.
Multithreading with Java and JavaScript #
Multithreading is supported when running JavaScript in the context of Java interoperability. The basic model of multithreaded execution supported by GraalVM is a “share-nothing” model that should be familiar to any JavaScript developer:
- An arbitrary number of JavaScript
Context
s can be created, but they should be used by one thread at a time. - Concurrent access to JavaScript objects is not allowed: any JavaScript object cannot be accessed by more than one thread at a time.
- Concurrent access to Java objects is allowed: any Java object can be accessed by any Java or JavaScript thread, concurrently.
A JavaScript Context
cannot be accessed by two or more threads, concurrently, but it is possible to access the same Context
from multiple threads using proper syncronization, to ensure that concurrent access never happens.
Examples #
The GraalJS unit tests contain several examples of multithreaded Java/JavaScript interactions. The most notable ones describe how:
- Multiple
Context
objects can be executed in multiple threads. - JavaScript values created by one thread can be used from another thread when proper synchronization is used.
- A
Context
can be accessed from multiple threads when proper synchronization is used. - Java concurrency can be used from JavaScript.
- Java objects can be accessed by multiple JavaScript threads, concurrently.