question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make API for Monitor work better with Java 8

See original GitHub issue

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;
    }
  }
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jsampsoncommented, Aug 12, 2018

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.

0reactions
jsampson2commented, Sep 15, 2018

FYI, I’ve posted the API for my complete redesign/rewrite of Monitor as a feature request here: #3265

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found