Code example for AtomicReference

0
    private static final String INIT_SCRIPT_PATH = "org/netbeans/gradle/project/resources/nb-init-script.gradle";
 
    private final AtomicReference<String> initScriptCache;
 
    public JavaInitScriptQuery() { 
        this.initScriptCache = new AtomicReference<String>(null);
    } 
 
    @Override 
    public String getInitScript() throws IOException {
        String result = initScriptCache.get();
        if (result == null) {
            result = StringUtils.getResourceAsString(INIT_SCRIPT_PATH, Charset.forName("UTF-8"));
            initScriptCache.compareAndSet(null, result);
            result = initScriptCache.get();
        } 
        return result;
    } 
}