I believe that I discovered a nasty little race condition in the 
org.jmol.viewer.ScriptManager class, which rears its head when 
multiple calls to JMolViewer.script() are made in rapid 
succession.  Obviously, this is most likely to occur when using 
JMolViewer programmatically, rather than from JavaScript (since 
JavaScript is too slow to call frequently enough).

In any case, the problem is that the ScriptManager.addScript() method 
first checks to see if the script execution thread is running; if 
not, it starts it.  The problem is that if two calls to addScript are 
made in rapid succession, multiple threads can be created, and the 
resulting scripts are run multiple times.  Not good!

The solution is to move the setting of scriptQueueRunning = true from 
the run() method of ScriptQueueRunnable to the start ScriptQueue 
function itself.  This eliminates the problem.

   private void startScriptQueue() {
     if (scriptQueueRunning)
       return;
       scriptQueueRunning = true;      <- set it here!
     queueThread = new Thread(new ScriptQueueRunnable());
     queueThread.start();
   }

The astute coder may not that this doesn't actually eliminate the 
race condition, it just mitigates it.  There is still a race 
condition between the check in the if statement and the setting of 
the variable.  I think (but I haven't verified) that this can be 
eliminated with a synchronized block, like

   private void startScriptQueue() {
     synchronized(this) {
            if (scriptQueueRunning)
              return;
              scriptQueueRunning = true;      <- set it here!
     }
     queueThread = new Thread(new ScriptQueueRunnable());
     queueThread.start();
   }

I don't have SVN write access, so I'll leave the final solution to 
one of you fine fellows.

        JR Schmidt


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Jmol-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jmol-developers

Reply via email to