Closed
Description
openedon Jun 18, 2017
The SafeBox
example for Monitor
is much more verbose than expected. We can do better:
public class SafeBox<V> {
private V value;
private final Monitor monitor = new Monitor();
private final Monitor.Guard valuePresent = guard(monitor, () -> value != null);
private final Monitor.Guard valueAbsent = guard(monitor, () -> value == null);
public V get() throws InterruptedException {
try (LockedMonitor ignored = monitor.autoEnterWhen(valuePresent)) {
V result = value;
value = null;
return result;
}
}
public void set(V newValue) throws InterruptedException {
try (LockedMonitor ignored = monitor.autoEnterWhen(valueAbsent)) {
value = newValue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Metadata
Assignees
Labels
No labels