Skip to content

Instantly share code, notes, and snippets.

@ardovic
Created July 5, 2019 13:48
Show Gist options
  • Select an option

  • Save ardovic/185bd21bf3175e9098992237094734b8 to your computer and use it in GitHub Desktop.

Select an option

Save ardovic/185bd21bf3175e9098992237094734b8 to your computer and use it in GitHub Desktop.
PendingUpdater construct, great for 2-key locks
public class PendingUpdater<T> {
private final Action<T> updateAction;
private boolean pendingUpdate;
private T value;
public PendingUpdater(Action<T> updateAction) {
this.updateAction = updateAction;
}
public void requestUpdate() {
if (value != null) {
updateAction.call(value);
} else {
pendingUpdate = true;
}
}
public void setValue(T value) {
this.value = value;
if (pendingUpdate) {
updateAction.call(value);
pendingUpdate = false;
}
}
public interface Action<T> {
void call(T t);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment