Actually Rhino exposes a "sync" function for this purpose, org.mozilla.javascript.Synchronizer is just the class implementing this feature and not meant to be used directly by scripts.

While you could do something like this in Nashorn I would advise against it. Thread-safety is not one of the primary design goals in Nashorn, and nowadays most people would agree that there are better and safer ways for working with threads. I suggest looking at Worker/Actor models where you have multiple script engines working together through message passing.

Hannes

Am 2013-10-11 10:45, schrieb Tal Liron:
Thanks!

Would it be possible to add this to mozilla_compat.js so that "new org.mozilla.javascript.Synchronizer(myFunction)" would work?

On 10/11/2013 03:31 PM, A. Sundararajan wrote:
There is a 'sync' function in a demo application (jconsole script plugin). This may be of use.

    var sync = function(func, obj) {
        if (arguments.length < 1 || arguments.length > 2 ) {
            throw "sync(function [,object]) parameter count mismatch";
        }

        var syncobj = (arguments.length == 2 ? obj : this);

        if (!syncobj._syncLock) {
            syncobj._syncLock = new Lock();
        }

        return function() {
            syncobj._syncLock.lock();
            try {
                func.apply(null, arguments);
            } finally {
                syncobj._syncLock.unlock();
            }
        };
    };

See also: https://blogs.oracle.com/nashorn/entry/nashorn_multi_threading_and_mt

-Sundar

On Thursday 10 October 2013 03:16 PM, Tal Liron wrote:
How does one create synchronized functions in Nashorn?

Rhino has this facility:

function myFunction() { ... }
myFunction = new org.mozilla.javascript.Synchronizer(myFunction)



Reply via email to