5 Replies Latest reply on Apr 28, 2006 2:33 AM by Bela Ban

    Double-checked locking idiom in Node.java

    Elias Ross Master


      This is basically broken for JVM 1.4 and earlier, although declaring children as volatile may fix it for most 1.4 JVM.

      protected Map children() {
       if (children == null) {
       synchronized(this) {
       // check again to see if children is null
       // to avoid possible race condition of another
       // thread having just exited this synchronized() section
       // that initialized children
       //
       if (children == null) {
       if (getFqn().isRoot()) {
       children = new ConcurrentReaderHashMap(64);
       } else {
       children = new ConcurrentReaderHashMap(4);
       }
       }
       }
       }
       return children;
       }
      


      Suggested solution:

      protected synchronized Map children() {
       if (children == null) {
       if (getFqn().isRoot()) {
       children = new ConcurrentReaderHashMap(64);
       } else {
       children = new ConcurrentReaderHashMap(4);
       }
       }
       return children;
       }
      


      Comments?