« December 2009
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today
XML

Blog::Navigation

GetJava Download Button
Get the Source
Personal Blog

Blog::Referers

Today's Page Hits: 610

Powered by Roller Weblogger.
« java.net project to... | Main | Namespaces in JavaSc... »
20051115 Tuesday November 15, 2005

Synchronized methods in JavaScript

How do we write synchronized methods in JavaScript? Rhino JavaScript shell supports a global function called sync. sync function creates a synchronized function from an existing function. The new function synchronizes on the "this" object of its invocation. Scripts executing in the shell have access this sync function. But, in Mustang's JavaScript engine, the sync function is available always (i.e., global scope is always initialized with this function).


var obj = { f: sync(function () { 
                     print('I am synchronized!');
               }
          };

// 'f' is a "synchronized" method.
obj.f(); 

If you just need mutual exclusion, then sync function is enough. But, what if you need wait, notify and notifyAll? We can add these as function properties to Object.prototype as shown below:

Object.prototype.wait = function() {
     var objClazz = java.lang.Class.forName('java.lang.Object');
     var waitMethod = objClazz.getMethod('wait', null);
     waitMethod.invoke(this, null);
}

Object.prototype.notify = function() {
     var objClazz = java.lang.Class.forName('java.lang.Object');
     var notifyMethod = objClazz.getMethod('notify', null);
     notifyMethod.invoke(this, null);
}

Object.prototype.notifyAll = function() {
     var objClazz = java.lang.Class.forName('java.lang.Object');
     var notifyAllMethod = objClazz.getMethod('notifyAll', null);
     notifyAllMethod.invoke(this, null);
}

We have added wait, notify and notifyAll to all JavaScript objects. Note that you can call the above methods only inside methods wrapped by sync. Or else you'll get IllegalMonitorStateException.

If you need multiple condition queues, read-write locks, semaphores, barriers and so on, then you can use java.util.concurrent API in JavaScript. (please refer to my earlier post )



( Nov 15 2005, 06:35:36 AM IST ) Permalink del.icio.us | furl | simpy | slashdot | technorati | digg

Comments:

Post a Comment:

Comments are closed for this entry.
Copyright (C) 2005, A. Sundararajan's Weblog