title: Java - Wait for Runnable to finish
body: <p>In my app, I have the following code running on a background thread:</p>
  
  <pre><code>MyRunnable myRunnable = new MyRunnable();
  runOnUiThread(myRunnable);
  
  synchronized (myRunnable) {
      myRunnable.wait();
  }
  
  //rest of my code
  </code></pre>
  
  <p>And MyRunnable looks like this:</p>
  
  <pre><code>public class MyRunnable implements Runnable {
      public void run() {
  
          //do some tasks
  
          synchronized (this) {
              this.notify();
          }
      }
  }
  </code></pre>
  
  <p>I want the background thread to continue after myRunnable has finished executing. I've been told that the above code should take care of that, but there are two things I don't understand:</p>
  
  <ol>
  <li><p>If the background thread acquires myRunnable's lock, then shouldn't myRunnable block before it's able to call notify() ?</p></li>
  <li><p>How do I know that notify() isn't called before wait() ?</p></li>
  </ol>