« January 2010
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: 865

Powered by Roller Weblogger.
« Asynchronous JavaScr... | Main | Java overloaded... »
20051021 Friday October 21, 2005

Concurrent Programming in JavaScript

As I've been repeatedly highlighting, Mustang (Java SE 6) includes Rhino JavaScript engine. How do we write multithreaded scripts? JavaScript engine allows Java method calls. So, we can use that to create script wrappers for Java platform classes as shown below:



// creates a java.lang.Runnable wrapping given script function.
function runnable(func) {
    return new java.lang.Runnable() {
        run: function() { func(); }
    }
}

// add 'thread' function property to Function prototype.
Function.prototype.thread = function() {
    var t = new java.lang.Thread(runnable(this));
    t.start();
    return t;
}

After including above code (using load function in jrunscript), we can easily create threads in script:



function myFunc() { 
//... code here...
}

// start 2 threads with myFunc as starting function
myFunc.thread();
myFunc.thread();

But, what about mutual exclusion/communication/co-operation between threads? You may want locks, condition variables etc. Well, you may write few script wrappers using java.util.concurrent API as shown below:


// creates a ReentrantLock
function lock() {
    return new java.util.concurrent.locks.ReentrantLock();
}

// runs given function after acquiring given lock and releases the lock
// after the function is finished. (in effect the script function is
// 'synchronized' in Java sense
Function.prototype.sync = function(lock) {
    lock.lock();
    try {
        this.call();
    } finally {
        lock.unlock();
    }
}


The caller of the above code would look like:


function myFunc() {
  // code here
}

// create a lock
var lk = lock();

// run myFunc in 'synchronized' way
// no two threads can run myFunc concurrently.
myFunc.sync(lk);


Note that I've not handled script functions with arguments. For eg., you may want to start a thread with a function that accepts arguments. As usual, that is left as an exercise to the reader :-)



( Oct 21 2005, 08:07:20 PM IST ) Permalink Comments [2] del.icio.us | furl | simpy | slashdot | technorati | digg

Comments:

This hints at my recent findings that current browser JavaScript engines are not multithreaded. Can you confirm this?

Note the following test, which schedules the same function to run 10 times at the exact same millisecond. In a multithreaded environment, you'd expect 10 threads (assuming the pool of available threads is large enough) to execute the code simultaneously. But when you run this test in IE6 and FF1.5, you see that the function invocations run serially, meaning that the last invocation doesn't happen anywhere near when it was supposed to run (in my test, it ran 0.3 seconds late)!

  window.logAlpha = function(iter) {
    var s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (var i = 0; i < s.length; i++) {
      $log.info(iter + ": " + s.charAt(i));
    }
  };

  var target = (new Date()).getTime() + 10000;
  for (var i = 0; i < 10; i++) {
    var now = (new Date()).getTime();
    var diff = target - now;
    setTimeout("logAlpha(" + i + ");", diff);
  }

Posted by Todd Volkert on August 11, 2006 at 10:03 PM IST #

It is quite possible that browser's JS engine(s) is /are single threaded -- or atleast execute setTimeout by event thread always. But, I am not very sure of this aspect though....

Posted by A. Sundararajan on August 14, 2006 at 05:22 PM IST #

Post a Comment:

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