Hi,
I'm having a bizarre problem using java threads. The program that
demonstrates the problem is fairly complex, but the problem comes down
to just a little thing - in the first implementation (which works) the
thread is created in the constructor. In the second, which does not
work, the thread is created in the start() method of the gzThread. I do
not know of any reason why the two are not exactly equivalent. Is there
any reason why they would work differently. See below for more gory details.
Thanks
Terry
gzThread = function() {
this.thread = new java.lang.Thread(new
java.lang.Runnable(this)); <-------------
this.thread.setDaemon( false );
this.isrunning = false;
}
gzThread.prototype = {
start: function() {
this.isrunning = true;
this.thread.start();
}
------ alternate implementation ---------------------
gzThread = function() {
this.isrunning = false;
}
gzThread.prototype = {
start: function() {
this.thread = new java.lang.Thread(new java.lang.Runnable(this));
this.thread.setDaemon( false );
this.isrunning = true;
this.thread.start();
}
Additional information
I'm using a gzbind function (based on John Resig's code) to bind a
function to on run.
Function.prototype.gzbind = function(object){
var fn = this;
return function(){
return fn.apply(object, arguments);
};
};
so
Test = function() {
this.value = "A test value";
this.gzthread = new gzThread();
this.gzthread.onrun = this.testrunner.gzbind(this);
In the run() method in gzThread there is a call to onrun
this.onrun(this);
In the case where the thread is created in the prototype I get this
exception:
js> org.mozilla.javascript.EvaluatorException: Can't find method
adapter2.onrun(adapter2). (wlib.js#62)
at
org.mozilla.javascript.tools.ToolErrorReporter.runtimeError(ToolErrorReporter.java:144)
at org.mozilla.javascript.Context.reportRuntimeError(Context.java:938)
at org.mozilla.javascript.Context.reportRuntimeError(Context.java:994)
at org.mozilla.javascript.Context.reportRuntimeError1(Context.java:957)
at
org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:166)
at
org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:3330)
at script(wlib.js:62)
where line 62 is the call to "this.onrun(this);"
And in one more bizarre twist if I change the code to just
"this.onrun()" with no parameter the second case works.
Is this a bug or am I missing some fundamental thing or is this a java
special case?
I would be happy to send along the code to demonstrate this if that
would help ....
Thanks again
Terry
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino