Re: Issues using "synchronized".
firstname.lastname@example.org wrote On 05/22/06 16:57,:
Unfortunately, there's a ton of code within that begin function that I
can't really share.
That's the point about asking for a "minimal" example.
You begin with the Mother Of All Classes, and you start
trimming away the parts that don't matter. This is akin to
what you tried to do when posting, but with an important
difference: As you trim, you keep on testing to be sure the
problem is still present. You keep on whittling until you
can whittle no more, and at the end you have something short
enough (and non-private enough) that you can reasonably
demonstrate the problem to Usenet. You don't need to post
the details of your Secret Sauce.
There's a side-benefit, too: You may whittle something
away and find that the problem vanished at the same time, and
this can draw your attention to the root cause. Google for
the "Wolf Fence method" of debugging.
I'm trying not to be snarky with you like you were with me, but I
thought I was reasonably clear in that I'm not looking for a specific
solution to the problem. I'm looking for any tips/pointers/ideas as to
what I might be doing wrong that could manifest itself in such a way
(which is why I stated "I'm wondering if there's something about the
synchronized keyword that I'm not aware of," suggesting I'm looking for
information about this stuff, not an exact solution to my problem).
I apologize for reading too hastily and assuming that you
wanted a solution to your problem. On the other hand, how can
I or anyone else possibly tell whether there's something about
the synchronized keyword that you're not aware of? I don't know
what your awareness encompasses.
I actually just noticed that the problem isn't that the code is never
getting to the notify (called as a result of the begin() function),
it's because it hits the notify before it hits the wait() in the main
Google for "lost wakeup." I think it's not your awareness
of synchronized that's at fault, but your understanding of how
to use wait() and notify()/notifyAll(). wait() should *always*
appear inside a loop that tests the "Should I wait?" condition.
A simple `if' is not sufficient; your code without even that
much is even less than not sufficient. Only a loop will do;
I suggest you return to your textbook and re-study the material
on how to use wait() and the notify() brethren.
What I don't understand now is why the added delay within the
main function (provided by the loop which obviously has a couple extra
zeros, since I didn't have the code right in front of me I just threw
that in quickly, the point of it should have been pretty clear)
Do you make your doctor guess what ails you? Do you play
Twenty Questions with your auto mechanic? What was "pretty
clear" was that when you wrote
I'm running the following code:
.... you were not being entirely candid. My snarkiness was
intended to shame and humiliate you (mildly) so you'd learn
a lesson about the difference between an accurate problem
report and a hand-waving approximation.
'tis said that in long-ago days when Kings were thought
of as semi-divine, there was a serious problem: When the King
fell ill, it was out of the question to permit an ordinary
mortal -- probably not even a noble! -- to touch the King's
Person, to view it unclothed, or to perform any of the other
unseemly acts that are the physician's stock in trade. So
they'd have a stand-in, a lesser personage who would imitate
the King's ailment and whom the doctor could examine without
fear of getting too familiar with the Presence. The doctor
would study the stand-in and thus make his diagnosis of what
ailed the King.
Granted, the doctors Back Then were not all that well
equipped to effect cures, and this dodge may actually have
helped the King's health by keeping the doctors at a safe
distance! But if one assumes modern medicine has made some
little progress over the centuries, do you think that this
examination by proxy is a good idea?
changing the order of execution to the point that if the delay is
there, I hit the wait() first, then the notify(). If the delay isn't
there, I hit the notify() before the wait(), which causes me to sit
waiting for a notify that has already happened.
Yah. You're misusing wait(). It's not about synchronized
at all, but about wait(). Back to the books.