Re: JavaScript not being blocked/synched by Applet init()
Hi Again,
"Richard Maher" <maher_rj@hotspamnotmail.com> wrote in message
news:guau5m$lb$1@news-01.bur.connect.com.au...
Hi Mark,
"Mark Space" <markspace@sbc.global.net> wrote in message
news:Er6Ol.22158$as4.21067@nlpi069.nbdc.sbc.com...
Joshua Cranmer wrote:
Although the SpiderMonkey engine is very much capable of doing
multithreaded execution, the DOM itself is not threadsafe and has to
be
modified from a single thread, so must browser JS ends up running in
one
thread. Hence the statement: "The JavaScript engines in all of today's
web browsers are conceptually single threaded with respect to the web
page."
And Richard Maher was able to create concurrent access to his Java
applet pretty much by accident.
I may have come across it by accident (and I think the goodness of
upward-compatibility dictates that the "old" behaviour should've been the
default - along the lines of the new classloader_cache parameter) but it
appears to have been definitely implemented on purpose. "Who uses the
functionality and why?" - I know not.
I don't think I really believe the
documentation in this regard.
If anyone can show me two JavaScript functions serving events
simultaneously
in the same browser-tab then I'd sure like to see it! (Or at least know
about it) onreadystatechange, onclick, mouseover, whatever - it can't be
done.
Or so I thought, at least prior to Java 6. I just stuck a
JSObject.getWindow(this).call("someFunc",null) into my Applet.init() -
without the "synchronized" - and it looks very muh to me that the
JavaScript
onload event and someFunc are executing in parallel :-o
The synchronization to make JavaScript
single thread seems buggy at best, non-existent at worse. And this is
from both IE and FF.
I'd disagree with you prior to Java 6, but as someone who will be
manipulating the DOM from JSObject.call()s in a separate Java thread in my
Applet, I am more than a tad concerned about developments.
In case it helps the discussion any, below is a modified version of my
earlier example that certainly appears to show two JavaScripr functions
executing in parallel. (The DOM "value" of the html field "next" ends up
"132" instead of "123")
Ok, "moving forward" can anyone show me similar behaviour outside of the
Applet.init()-JSObject interaction environment? (The work-around, as Mark
suggested, being to stick "synchronized" on the applet methods and call-back
onto the Applet as soon as possible to synch with the return from init() )
Are all JSObject.call()s prone to threading issues rather than all being
queued to the EDT? This'd be disaterous - please advise!
Would a further setTimer(0,"pleaseFireMeOnEDT()") work-around mechanism be
required?
Cheers Richard Maher
Console output
============
Before fromInit call
in getNum 1
in getNum 2
in getNum 3
in getNum 4
After fromInit call
Before sleep call
After sleep call
Sleeper.java
===========
import java.applet.Applet;
import netscape.javascript.JSObject;
import netscape.javascript.JSException;
import java.lang.InterruptedException;
public class Sleeper extends Applet {
private int myNum = 0;
private JSObject browser;
public void init() {
super.init();
try {
browser = JSObject.getWindow(this); }
catch (netscape.javascript.JSException e) {
e.printStackTrace(); }
catch (Exception e) {
e.printStackTrace(); }
System.out.println("Before fromInit call");
browser.call("fromInit", null);
System.out.println("After fromInit call");
System.out.println("Before sleep call");
try {
Thread.sleep(5000);
}
catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("After sleep call");
myNum = 33;
}
public int getNum(){
int i = myNum++;
System.out.println("in getNum " + myNum);
return i;
}
public void destroy ()
{
System.out.println("Checked - out");
super.destroy();
}
}
sleeper.html
=========
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<meta name="author" content="Richard Maher"/>
<meta name="description" content="JS Function and Applet Test"/>
<head>
<style>
body
{
margin: 0px;
background-color: white;
color: Black;
font-family: times;
font-size: 16px;
border: medium ridge;
}
</style>
<script type="text/javascript">
var tmp;
function load() {
var lclNum;
var chan;
tmp ="Stage 1";
document.display.next.value = "1";
try {
chan = document.getElementById("Sleeper");
lclNum = chan.getNum();
lclNum = chan.getNum();
lclNum = chan.getNum();
}
catch (err) {
alert("In catch " + err.description);
}
if (chan == null) alert("chan is null");
alert(chan.getNum() + " tmp = " + tmp);
document.display.next.value += "2";
alert("onLoad tmp was " + tmp);
tmp ="Stage 3";
}
function fromInit(){
document.display.next.value += "3";
var oldTmp = tmp;
tmp = "Stage 2";
alert("fromInit tmp = "+tmp+" oldTmp = "+oldTmp);
}
</script>
</head>
<body onload="load();">
<br /><h2>Test it</h2><hr /><br />
<form name="display" style="margin-left: 100px;">
<input
type="text"
style="text-align: Left;"
name="next"
size=10
/>
</form>
<script type="text/javascript">
var myDef;
if (navigator.appName == "Microsoft Internet Explorer")
myDef =
'<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" ' +
'width= "0" height= "0" id="Sleeper">'
+
'<param name="code" value="Sleeper">'
+
'<param name="mayscript" value="true">' +
'<param name="scriptable" value="true">' +
'</object>'
else
myDef =
'<object classid="java:Sleeper.class" '
+
'type="application/x-java-applet" ' +
'width= "0" height= "0" id="Sleeper">'
+
'<param name="code" value="Sleeper">'
+
'<param name="mayscript" value="true">' +
'<param name="scriptable" value="true">' +
'</object>'
document.write(myDef);
</script>
</body>
</html>