peggy:
must use spawn by many thread? if use many thread , how can i know if
error in one thread...
I'm not sure what the actual purpose of your code is (do you actually
want to spawn two threads and invoke f() from both of them?) but the
reason you are seeing "undefined;undefined" is because you are passing
to obj.thread1() the result of calling f(1, 2).
test example:
var obj = { thread1 : sync(f)};
function f(a, b) {
java.lang.Thread.sleep(2000);
print(a + ";" + b);
var threadName = java.lang.Thread.currentThread().getName();
var threadID = java.lang.Thread.currentThread().getId()
print(threadID + "-" + threadName);
}
obj.thread1(f(1, 2));
obj.thread1(f(3, 4));
obj.thread1 is just a synchronized version of f (synchronizing on obj).
You are passing the result of f(1, 2) to obj.thread1, so it's like
you're doing:
obj.thread1(undefined);
where obj.thread1 then synchronizes and calls f.
So probably what you meant was:
obj.thread1(1, 2);
obj.thread1(3, 4);
sync() does not spawn a thread though; all it does is make the function
synchronize on the "this" object that it is called on.
If you want to spawn two threads, where one thread will do f(1, 2) and
the other will do f(3, 4), and both invocations of f are synchronized on
obj, then do something like this:
var obj = {
spawnSyncAndRun: function(g) {
var t = new Thread(function() {
sync(g).call(obj);
});
t.run();
}
};
function f(a, b) {
...
}
obj.spawnSyncAndRun(function() { f(1, 2); });
obj.spawnSyncAndRun(function() { f(3, 4); });
So you can pass any function to obj.spawnSyncAndRun() and it will spawn
a new thread, synchronize on obj, and then run the function you passed it.
The sync(g).call(obj) line is to make sure that sync() is run with obj
as its "this" value.
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino