title: What is a Condition Variable in java?
body: <p><strong>Q1.</strong> What is a condVar in Java? If I see the code below, does a condition variable necessarily have to be within the '<strong>mutex.acquire()</strong>' and '<strong>mutex.release()</strong>' block?</p>
  
  <pre><code>public void put(Object x) throws InterruptedException {
     mutex.acquire();
     try {
        while (count == array.length)
        notFull.await();
        array[putPtr] = x;
        putPtr = (putPtr + 1) % array.length;
        ++count;
        notEmpty.signal();
    }
    finally {
       mutex.release();
    }
  }
  </code></pre>
  
  <p>I have three threads <strong>myThreadA</strong>, <strong>myThreadB</strong>, <strong>myThreadC</strong> running which call the same function <strong>commonActivity()</strong> which triggers the function <strong>myWorkReport()</strong> e.g.</p>
  
  <pre><code>public void myWorkReport(){
      mutexMyWork.acquire();
      try{
           while(runMyWork){
               doWork();
               conditionMyWork.timedwait(sleepMyWork);
           }
      }
      finally{
         mutexMyWork.release()
      }    
  }
  
  public void commonActivity(){
      try{
          conditionMyWork.signal(); 
      }finally{
          //cleanup
      }   
  }
  
  public void myThreadA(){
      mutexA.acquire();
      try{
          while(runningA){    //runningA is a boolean variable, this is always true as long as application is running
              conditionA.timedwait(sleepA);
              commonActivity();
          }
      }
      finally{
          mutexA.release();
      }
  }
  
  
  public void myThreadB(){
      mutexB.acquire();
      try{
          while(runningB){    //runningB is a boolean variable, this is always true as long as application is running
              conditionB.timedwait(sleepB);
              commonActivity();
          }
      }
      finally{
          mutexB.release();
      }
  }
  
  public void myThreadC(){
      mutexC.acquire();
      try{
          while(runningC){    //runningC is a boolean variable, this is always true as long as application is running.
              conditionC.timedwait(sleepC);
              commonActivity();
          }
      }
      finally{
          mutexC.release();
      }
  }
  </code></pre>
  
  <p><strong>Q2.</strong> Is using timedwait a good practice. I could have achieved the same by using sleep(). If using sleep() call is bad, Why?</p>
  
  <p><strong>Q3.</strong> Is there any better way to do the above stuff?</p>
  
  <p><strong>Q4.</strong> Is it mandatory to have <strong>condition.signal()</strong> for every <strong>condition.timedwait(time);</strong></p>