Interesting. Didn't know it was a known problem with java 1.4.
Lazy initialization was used exactly because we don't want dispatch
threads to start unless they are needed. So ... per Wikipedia article
this is a legacy JDK problem and going forward we should simply be
using "volatile" keyword, which I think we should.
Andrus
On Mar 30, 2007, at 1:10 AM, Peter Karich wrote:
> Hello!
>
> I profile my application with netbeans profiler.
> And I figured out that there are 12 (!) EventManager's DispatchThreads
> started.
> All are in the 'waiting' state.
> I have no problems with that :-)
> But could it be that this code is problematic:
>
> public static EventManager getDefaultManager() {
> if (defaultManager == null) {
> synchronized (EventManager.class) {
> if (defaultManager == null) {
> defaultManager = new EventManager(2);
> }
> }
> }
> return defaultManager;
> }
>
> See the problems with double checked locking here:
>
> http://en.wikipedia.org/wiki/Double-checked_locking
>
> Particularly the section:
> "One of the dangers of using double-checked locking in J2SE 1.4 ..."
>
> A simple solution can be:
>
> private static EventManager defaultManager = new EventManager(2);
> public static EventManager getDefaultManager() {
> return defaultManager;
> }
>
> OR a full synchronized method.
>
> Peter.
>
This archive was generated by hypermail 2.0.0 : Fri Mar 30 2007 - 08:40:35 EDT