Make API for Monitor work better with Java 8
See original GitHub issueThe 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;
}
}
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (7 by maintainers)
Top Results From Across the Web
API Design With Java 8 - DZone
Learn to be a better Java programmer by mastering Java 8 API design, exposing a well-designed API, making sure that client code can...
Read more >The Key Aspects of Java's Monitoring and Management APIs
The Java platform provides many tools for monitoring and management. Explore the key features now.
Read more >15 Best Java Performance Monitoring Tools & Software [2022]
Discover top tools available to monitor Java Virtual Machine performance! The best for monitoring and analyzing JVM metrics to ensure Java app performance....
Read more >Top 8 Java Performance Monitoring and Optimization Tools
Java has become ubiquitous for web application development. Here's a list of our most recommended Java performance monitoring tools.
Read more >A Guide to Java Streams in Java 8: In-Depth Tutorial ... - Stackify
To understand this material, you need to have a basic, working knowledge of Java 8 (lambda expressions, Optional, method references).
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Original author of Monitor here (no longer at Google but still occasionally contributing). Just noticed this issue.
FYI, we did at least implement “creation of Guards using a BooleanSupplier” a little while back. It’s an instance method called newGuard. @cpovirk, I wonder if it’s time to change the examples in the class javadoc to use newGuard and save 8 lines of code. 😀 There are also a couple of typos in the javadoc for the method itself that could be fixed.
Beyond that, I actually did a complete redesign/rewrite a few years ago that’s both easier to use (lambdas for all guards and tasks) and higher performance (built on primitives instead of wrapping a lock), but it’s complicated enough that no one at Google has found the time to review it. 😉 Perhaps I should just put it up on GitHub myself to crowdsource the review.
FYI, I’ve posted the API for my complete redesign/rewrite of Monitor as a feature request here: #3265