2

What is the recommended way to use Graal.js in a multi-threaded application (such as per servlet request)? We are using Graal.js like this

jsContext = Context.newBuilder("js").allowAllAccess(true).build();
bindings = jsContext.getBindings("js");
jsContext.eval("js", jsCodeString);

Should we have a unique Context/binding for each executing thread. This can be accomplished through a pool of Context/Binding pairs or by using threadlocal. Is this the proper way to do this?

Oleg Šelajev
  • 3,530
  • 1
  • 17
  • 25
adamM
  • 1,116
  • 10
  • 29

1 Answers1

2

You should not access one Context from several threads at the same time. So, the solutions that you propose (a pool of Contexts or thread-local Contexts) are valid ways to do that.

graalvm/graaljs repository contains some threading-related examples. You can see the usage of a thread-local Context there (in ExecutorsTest).

FYI: There is a blog-post about multi-threading with Graal JavaScript that may clarify further questions that you may have.

Jan Štola
  • 1,118
  • 1
  • 7
  • 6