Code example for AtomicReference

Methods: compareAndSet, get

0
     * @return The document builder 
     * @throws ParserConfigurationException for errors during creation 
     */ 
    private static DocumentBuilder getCreationDocumentBuilder()
        throws ParserConfigurationException { 
        final DocumentBuilder current = DOCUMENT_BUILDER.get();
        if (current != null) {
            return current;
        } 
        final DocumentBuilder newBuilder = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder();
        if (DOCUMENT_BUILDER.compareAndSet(null, newBuilder)) {
            return newBuilder;
        } else { 
            return DOCUMENT_BUILDER.get();
        } 
    } 
 
    static { 
        final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
        xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
        XML_INPUT_FACTORY = xmlInputFactory;