title: Proper lock/unlock usage for Java Reentrant locks
body: <p>I have a homework assignment called "The H2O problem" where I'm supposed to implement a class called H2OBarrier which has 3 methods. </p>
  
  <ul>
  <li>HReady, a method called when a hydrogen atom (thread) is ready</li>
  <li>OReady, a method called when an oxygen atom (thread) is ready</li>
  <li>makeWater, a method called when 2 hydrogen atoms and one oxygen atom are ready</li>
  </ul>
  
  <p>I'm supposed to do this using Java Reentrant locks and Conditions.</p>
  
  <p>This is my code so far, and I'm wondering if I'm properly using lock and unlock.</p>
  
  <pre><code>public class H2OBarrier {
  
  int hCount;
  int oCount;
  
  Lock lock = new ReentrantLock();
  Condition hWait = lock.newCondition();
  Condition oWait = lock.newCondition();
  
  public void HReady() {
      lock.lock();
      hCount++;
  
      try {
          hWait.await();
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  
  }
  
  public void OReady(){
      lock.lock();
      oCount++;
  
      try {
          while(hCount&lt;2 &amp;&amp; oCount&lt;1){
              oWait.await();
          }   
      } catch (InterruptedException e) {
          e.printStackTrace();
      } finally {
          makeWater();
      }
  
  
  }
  
  public void makeWater(){
      hCount--;
      hWait.signal();
      lock.unlock();
  
      hCount--;
      hWait.signal();
      lock.unlock();
  
      oCount--;
      oWait.signal();
      lock.unlock();
  
  }
  
  } 
  </code></pre>
  
  <p>Should I be calling unlock() anywhere other than in my makeWater method? The whole flow of the program seems to make pretty logical sense to me, I'd just like to make sure what I'm doing looks correct overall. </p>