Synchronized functions
Tal Liron
tal.liron at threecrickets.com
Fri Oct 11 02:02:43 PDT 2013
Also, wouldn't this shorter version work?
function synchronize(fn) {
var lock = new java.util.concurrent.locks.ReentrantLock()
return function() {
lock.lock()
try {
return fn.apply(this, arguments)
}
finally {
lock.unlock()
}
}
}
On 10/11/2013 04:45 PM, Tal Liron wrote:
> 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)
>>
>
More information about the nashorn-dev
mailing list