Re: Chrome selects wrong thread for JS-Java upcall on 2nd(ish) Applet
instance
On 10/30/2010 4:25 AM, Richard Maher wrote:
What appears to go wrong with Chrome on the 2nd -(usually the 2nd but then
it might work/fail for additional tabs - the first page always works)- tab
with an applet invoking page is that the isAuthorized() upcall gets executed
on the "thread applet-tier3Client/Tier3Application-2" Thread. The same
Thread that init() was/is executing on!!!
Now my understanding of the Java threading model is that, to support
recursion, a Thread cannot lock/mutex/synchronize itself out of an object.
So my "synchronized" init() and isAuthorized() methods on the Applet 2
instance don't amount to a hill o' beans and my authorization check occurs
*before* I've determined if the user is authorized and I've had a chance to
set the variable :-(
Look, I know I shot my mouth off before about this being a race condition
where the isAuthorized() was being called *before* the init() but that was
because I couldn't cope with the concept of synchronized Applet instance
methods executing at the same time.
Please help if you can.
Cheers Richard Maher
It might help you to add some sort of logging to the "init()" and
"isAuthorized()" methods, to trace when they start and when they end.
In either case, you have a mistaken conception that Chrome can
"interrupt" a running thread (the thread which is running your init
method) to do some other task.
What you may need to do is this, although without seeing your code, I
can only guess at your problem.
class MyApplet extends Applet {
private final Object sync = new Object();
private boolean initFinished;
public void init() {
initIfNecessary();
}
public void initIfNecessary() {
synchronize (sync) {
if (!initFinished) {
// do initializatation
initFinished = true;
}
}
}
public boolean isAuthorized() {
initIfNecessary();
return /* check for authorization */
}
}
I suspect that while this may "resolve" your issue, that there is some
other underlying misunderstanding of yours at the core of this problem,
which can only be corrected if you post an SSCCE (see
http://virtualinfinity.net/sscce.html)
Another problem you may run into is Java->JS calls. Documentation on
how to make those calls threadsafe is difficult to find. I'm not even
sure it *is* possible (one can only hope the browser itself has a JS
engine which is thread-safe).
HTH,
Daniel.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>