title: Java: Wait a boolean to become true
body: <p>I implemented a task which current thread will wait until previous work is done.</p>
  
  <p>Code:</p>
  
  <pre><code>    while (!taskContext.isPreviousWorkDone()) {
          try {
              Thread.sleep(100);
          } catch (InterruptedException e) {
              LOGGER.error("Wait failed", e);
          }
      }
      doSomething();
  </code></pre>
  
  <p>Another thread will set <code>isPreviousWorkDone</code> to true and then we can run <code>doSomething()</code>. But I think my code is bad:</p>
  
  <ol>
  <li>we check that every <code>100ms</code>, and it may cost some CPU resource.</li>
  <li>It's hard to add a wait timeout. (What if <code>isPreviousWorkDone</code> never become true?)</li>
  </ol>
  
  <p>So, how to implement such a wait properly?</p>