The answer is "don't call it onrun".
The java adapter has its own method called onrun() so it finds that. If
you change the name to onxrun and then add the line
>for (n in this) print(n);
before the call to
>this.onrun(this)
you will see there are BOTH "onrun" and "onxrun" properties. And it
works in both cases.
Name collision.
Terry Braun wrote:
Should I file this as a bug?
Terry Braun wrote:
Ok, here is the whole thing
---- cut ------------
importPackage(java.io);
importPackage(java.util);
importPackage(java.net);
importPackage(java.util.concurrent);
importPackage(java.lang);
Function.prototype.gzbind = function(object){
var fn = this;
return function(){
return fn.apply(object, arguments);
};
};
gzThread = function() {
this.isrunning = false;
// here the program works uncomment the next two lines
// this.thread = new java.lang.Thread(new
java.lang.Runnable(this));
// this.thread.setDaemon( false );
}
gzThread.prototype = {
start: function() {
// creating a thread here does not work
this.thread = new java.lang.Thread(new java.lang.Runnable(this));
this.thread.setDaemon( false );
this.isrunning = true;
this.thread.start();
return this;
},
stop: function() {
if (this.isRunning == true ) {
this.isRunning = false;
this.thread.interrupt();
this.thread.join(1000);
if (this.onstop ) {
this.onstop();
}
}
},
current: function() {
return java.lang.Thread.currentThread();
},
run: function() {
if ( this.onrun == null || this.isRunning == true) {
return this;
}
this.isRunning = true
try {
this.onrun(this);
} catch (ioe ) {
var w, m;
if ( ioe.rhinoException
&& ioe.rhinoException instanceof
org.mozilla.javascript.WrappedException
&& (w = ioe.rhinoException.getWrappedException())
&& (s = w.getMessage() )
&& s == "sleep interrupted") {
} else {
if ( ioe.rhinoException ) {
ioe.rhinoException.printStackTrace();
} else if ( ioe.javaException ) {
ioe.printStackTrace();
}
}
}
return this;
}
}
Test = function() {
this.value = "A test value";
this.gzthread = new gzThread();
this.gzthread.onrun = this.testrunner.gzbind(this);
}
Test.prototype = {
testrunner: function() {
print(this.value);
},
start: function() {
print("Starting ....");
this.gzthread.start();
}
}
var t = new Test();
t.start();
[email protected] wrote:
On Mar 7, 8:59 pm, Terry Braun <[email protected]> wrote:
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
It seems like what you're doing is fairly complicated, and without the
full implementation of gzThread, it's impossible to diagnose what's
going on. For example, you haven't included the run: function() {} in
your prototype, so it's impossible tell accurately.
I'd try reducing your example to the smallest possible case you can,
without using the gzbind() or any other clever window dressing that
might confuse things.
For example, I was able to get the following to work
function Starter() {
this.isRunning = false
}
Starter.prototype = {
start: function() {
this.thread = new java.lang.Thread(new
java.lang.Runnable(this))
this.thread.daemon = false
this.thread.start()
this.isRunning = true
},
run: function() {
if (this.onrun) {
this.onrun()
}
}
}
var starter = new Starter()
starter.onrun = function() {
java.lang.System.out.println("I'm running. Catch me if you can!")
}
starter.start()
***
saruman:jsfun cowboyd$ java -jar lib/rhino-1.7R1.jar starter.js
I'm running. Catch me if you can!
So it can be done. I'd start from this simplest case, and work it into
your framework.
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino