is multithreading possible in rhino? #1496
Replies: 4 comments 3 replies
-
Hi, don't have a clearcut answer for you, but:
Hope this provides some useful info |
Beta Was this translation helpful? Give feedback.
-
Thanks for your answer, Paul. This is in the category of "we should write
some documentation for this."
JavaScript (the language) doesn't really contemplate multi-threading, and
almost every JavaScript engine operates under the assumption that
everything is single-threaded. (Contrast this to Java, Go, Rust, etc which
have an explicit part of the specification that talks about how things are
supposed to work across threads.)
Over the years, people have written multi-threaded Java programs in Rhino,
which is why it has, for example, a "synchronized" keyword available as an
extension, but in general I don't think that what's there is really
thread-safe in all cases.
My official advice for multi-threading in Rhino is to ensure that each
script operates in one thread at a time. That means not to run a script and
then try to execute multiple functions at the same time in different
threads, for example.
However, you can have as many separate scripts running in a multi-threaded
process as you'd like, as long as each individual script is kept
single-threaded. That's by far the safest way and the only one that I can
be assured is thread safe in all cases.
…On Wed, Jun 19, 2024 at 10:15 AM Paul Bakker ***@***.***> wrote:
Hi, don't have a clearcut answer for you, but:
- latest versions of Rhino do support promises
- Javascript code inside Rhino runs within a Context, which is tied to
Thread
- depending on how you use Rhino, you may have direct access to Java,
which with allow you to interact with any threading-related Java API
- while Javascript doesn't have a synchronized keyword, Rhino does
have an Synchronized class, which, if accessible inside Javascript, can be
used to make methods synchronize on their this, see
https://www-archive.mozilla.org/rhino/apidocs/org/mozilla/javascript/synchronizer
(exposed in the shell by default as sync
- JavaScript objects themselves are NOT fully thread safe, see #1476
(comment)
<#1476 (comment)>
for some more info
Hope this provides some useful info
—
Reply to this email directly, view it on GitHub
<#1496 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAD7I24HQHNHKRKONFE2LYTZIG4C7AVCNFSM6AAAAABJQYYDZOVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TQMRQGMYDA>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Agreed, hence I had already added the Any chance you could elaborate a bit on your knowledge on what we do have wrt mutlithreading support? From the linked PR discussion it sounds like adding/removing properties on objects is threadsafe, but mutating a property value isn't? Or am I completely misreading that discussion? |
Beta Was this translation helpful? Give feedback.
-
I'd just like to throw this into the mix: https://rhino.github.io/tutorials/scripting_java/#javascript-functions-as-java-interfaces js> t = java.lang.Thread(function () { print("\nrunning"); });
Thread[Thread-0,5,main]
js> t.start()
js>
running I've recently been experimenting with using the async methods of java.http.net.HttpClient from Rhino, and I'm using javascript arrow functions to implement the functional interfaces from the CompletableFuture API to handle the responses. It seems to be working, but I'm programming in a functional style, so I'm usually not accessing the same javascript objects from multiple threads. If I do need to share something between threads, then I would use something from java.util.concurrent rather than javascript objects. |
Beta Was this translation helpful? Give feedback.
-
Hi I'm trying to execute one JS Rhino script before another inside of another application (Cameo/MagicDraw), so I'm experimenting with asynchronous programming and multithreading. Is this supported by rhino?
I know it's possible in Java if you specify a shared object and calling synchronized(object) {//thread body} and inside have the waiting thread call .wait() and the worker thread call .notify() so that I can run worker thread/file before the waiting thread/file. However rhino seems to not recognize the synchronized() keyword and syntax.
Any help would be greatly appreciated and I'm open to other ideas on how I can specify one script to run before another (because I need values and calculations performed in 1 script in the other script) maybe this is a longer way to achieve this.
Beta Was this translation helpful? Give feedback.
All reactions