title: The right way to synchronize on non-final object in Java
body: <p>I'm trying to protect an object with a lock.</p>
  
  <p>I didn't choose mutexes because of ugly syntax of "try..catch".</p>
  
  <p>Browsing stackoverflow, I came to a conclusion that this is how to properly achieve my goal:</p>
  
  <pre><code>class MyClass {
      private final Object lock = new Object();
      private Channel channel = null;
  
      public void setChannel() {
          synchronized (lock) {
              channel = new Channel();
              synchronized (channel) {
                  // setup channel
              }
          }
      }
  
      public void unsetChannel() {
          synchronized (lock) {
              synchronized (channel) {
                  channel.close();
              }
          channel = null;
          }
      }
  
      public boolean isSet() {
          synchronized (lock) {
              if (channel == null)
                  return false;
              synchronized (channel) {
                  return channel.isActive();
              }
          }
      }
  }
  </code></pre>
  
  <p>But it seems ugly and hard to read...</p>
  
  <p>How can I improve readability of my solution?</p>