Class CloseableMonitor


  • public class CloseableMonitor
    extends Object
    Wraps a Monitor with methods that can be used with try-with-resources.
    • Field Detail

      • monitor

        public final com.google.common.util.concurrent.Monitor monitor
    • Constructor Detail

      • CloseableMonitor

        public CloseableMonitor()
    • Method Detail

      • enterWhenUninterruptibly

        public CloseableMonitor.Hold enterWhenUninterruptibly​(com.google.common.util.concurrent.Monitor.Guard condition)
        try-with-resources wrapper for enterWhenUninterruptibly. For example:
         final Monitor.Guard guard = new Monitor.Guard(monitor.monitor) {
             @Override
             public boolean isSatisfied() {
               assertThat(monitor.monitor.isOccupied()).isTrue();
               return state;
             }
         };
        
         try (CloseableMonitor.Hold h = monitor.enterWhenUninterruptibly(guard)) {
           // Do stuff
         }
         // Monitor is automatically released
         
      • enter

        public CloseableMonitor.Hold enter()
        try-with-resources wrapper for enter. For example...
        
         try (CloseableMonitor.Hold h = monitor.enter()) {
           // Do stuff
         }
         // Monitor is automatically released