Re: Issues using "synchronized".
frick.jeremiah@gmail.com wrote On 05/22/06 14:26,:
Hello everyone,
I'm running the following code:
[see up-thread]
The problem is this. The first time through the software executes as
expected and the wait completes as expected.
In the code you've shown, there's no reason to expect
that the wait() completes at all. You didn't disclose what
the begin() method does, nor for that matter the getRes()
method. As far as we can see, there are no calls to notify()
or notifyAll() anywhere in the code, hence no reason to expect
that the wait() would awaken other than "randomly."
The second time, though,
the task seems to hang while waiting.
If I put a delay loop between the two sections above (replace //
******** // with for( int j = 0; j < 10000000000000; j++ ); ), then the
second piece of software completes properly as well.
Since this loop won't even compile, I hope you'll pardon
me for harboring doubts when you say it makes the program behave
"properly."
Since the class should go out of scope after the first sequence
completes and then is created new with the "new" keyword in the second
sequence I can't figure out why the delay loop is fixing the problem.
I'm wondering if there's something about the synchronized keyword that
I'm not aware of.
The problem is almost certainly in the code you decided
to withold, not in the code fragments you revealed. (This is
sadly typical: Somebody who's baffled by a piece of code takes
it upon himself to decide which chunks are relevant or not,
even though the very fact of bafflement suggests he's in the
poorest possible position to make such decisions.) Please try
posting a
minimal
complete
compilable
example that demonstrates the problem, and maybe somebody will
be able to help you out.
--
Eric.Sosman@sun.com