Promise<T> (boolean checkFreshness) { Promise<T> promise = ref.get(); if (promise == null) { if (!ref.compareAndSet(null, promise = new AsyncPromise<>())) { return ref.get(); } } else { Promise.State state = promise.getState(); if (state == Promise.State.PENDING) { // if current promise is not processed, so, we don't need to check cache state return promise; } else if (state == Promise.State.SUCCEEDED) { if (!checkFreshness || isUpToDate()) { return promise; } if (!ref.compareAndSet(promise, promise = new AsyncPromise<>())) { Promise<T> valueFromAnotherThread = ref.get(); while (valueFromAnotherThread == null) { if (ref.compareAndSet(null, promise)) { return getPromise((AsyncPromise<T>)promise); } else { valueFromAnotherThread = ref.get(); } } return valueFromAnotherThread; } } } return getPromise((AsyncPromise<T>)promise); }