private void safeInvalidate(RawJobBrokerKey key) { Lock lock = this.invalidationLock.get(key); lock.lock(); try { Object obj = this.sharedResourceCache.getIfPresent(key); if (obj != null && obj instanceof ResourceEntry && !((ResourceEntry) obj).isValid()) { this.sharedResourceCache.invalidate(key); ((ResourceEntry) obj).onInvalidate(); } } finally { lock.unlock(); } }
/** * This method should guarantee that if all callers accessing the resource using this method then the object is * returned atomically with respect to any validity state change. * * This is to avoid race conditions in cases where the state is changed when getting the resource. Some examples are * resources that can only be used a certain number of times. * * @return null if the object is not valid, otherwise the valid object */ default T getResourceIfValid() { if (isValid()) { return getResource(); } else { return null; } } }
T resource = ((ResourceEntry<T>) obj).getResourceIfValid();
T resource = ((ResourceEntry<T>) obj).getResourceIfValid();
private void safeInvalidate(RawJobBrokerKey key) { Lock lock = this.invalidationLock.get(key); lock.lock(); try { Object obj = this.sharedResourceCache.getIfPresent(key); if (obj != null && obj instanceof ResourceEntry && !((ResourceEntry) obj).isValid()) { this.sharedResourceCache.invalidate(key); ((ResourceEntry) obj).onInvalidate(); } } finally { lock.unlock(); } }
/** * This method should guarantee that if all callers accessing the resource using this method then the object is * returned atomically with respect to any validity state change. * * This is to avoid race conditions in cases where the state is changed when getting the resource. Some examples are * resources that can only be used a certain number of times. * * @return null if the object is not valid, otherwise the valid object */ default T getResourceIfValid() { if (isValid()) { return getResource(); } else { return null; } } }