On 19/08/2011 6:40 PM, Clemens Eisserer wrote:
Found a mistake, corrected version at:
http://cr.openjdk.java.net/~ceisserer/7080700/webrev.02/
It ended up with numAppContexts=2 when class-initialization was done,
although there was only one context.
The original code is rather strange. The static field will be default
initialized to zero, the AppContext constructor will increment it to 1, then
the static block explicitly sets it to one.
It would be cleaner/clearer if the field were declared and initialized
before the static block.
It's not clear to me why this case needs to be handled specially. I also
can't tell what code would be calling getAppContext under those conditions.
From what I understand its a fast-path. For normal Swing Applications, no
AppContexts are ever created (except the "system" context when
AppContext.class is loaded -> numAppContexts=1), so the code avoids the
look-up. I may have misunderstood it of course ;)
That's an uninteresting case from a data-race perspective though because the
counter is only ever written once.
However I now wonder about how often this gets called in a Swing app? If
this is a frequent call then the overhead of the atomic might be
significant. Perhaps the Swing folk should be evaluating this change as well?
Cheers,
David
Thanks, Clemens