title: how can an AsyncTask wait for another without deadLock
body: <p>I'm trying to implement 2 Async Tasks where one should wait for the other to finish.</p>
  
  <p>I make a test case for it, that looks like this:</p>
  
  <pre><code>public class MainActivity extends Activity{
  
      public static boolean SYNCING = false;
      public static Object LOCK = new Object();
  
      public static void setSync(){
          Log.i(TAG, "setting Sync=true");
          synchronized (LOCK) {
              SYNCING = true;
          }
      }
  
      public static void waitForSync(){
          Log.i(TAG, "waiting for Sync");
          synchronized (LOCK) {
              try{
                  while (SYNCING){
                      LOCK.wait();
                  }
              } catch (InterruptedException ignore){}
          }
      }
  
      public static void setSyncFinish(){
          Log.i(TAG, "setSyncFinish to notify all");
          synchronized (LOCK) {
              SYNCING = false;
              LOCK.notifyAll();
          }
      }
  
      public class WaitingOnLoaderTask extends AsyncTask&lt;Void, Void, Void&gt;{
  
          @Override
          protected Void doInBackground(Void... params) {
              Log.i(TAG, "doInBackground of WaitingForLoaderTask");
  
              try {
                  Thread.sleep(500);
              } catch (InterruptedException e) {
  
              }
  
              waitForSync();
              Log.i(TAG, "finishedWaiting!");
              return null;
          }
  
      }
  
      public class LoaderTask extends AsyncTask&lt;Void, Void, Void&gt;{
  
          long mRuntime;
          int mTimes;
  
          public LoaderTask(long runtime, int times){
              mRuntime = runtime;
              mTimes = times;
          }
  
          @Override
          protected Void doInBackground(Void... params) {
              while(mTimes &gt;= 0){
                  Log.i(TAG, "sleeping for " + mTimes + " times for " + mRuntime + " milliseconds");
                  mTimes--;
                  try {
                      Thread.sleep(mRuntime);
                  } catch (InterruptedException e) {
                      Log.e("MainActivity", "Interrupted", e);
                  }
              }
  
              setSyncFinish();
  
              return null;
          }
  
      }
  }
  </code></pre>
  
  <p>But I can't get the <code>WaitingOnLoaderTask</code> to wait correctly. if I run this in onCreate</p>
  
  <pre><code>new WaitingOnLoaderTask().execute((Void[])null);
  new LoaderTask(1000, 5).execute((Void[])null);
  </code></pre>
  
  <p>I get a logOutput like this (a DeadLock happens):</p>
  
  <pre><code>03-08 18:17:23.309: I/myUniqueLogTag(4432): setting Sync=true
  03-08 18:17:23.317: I/myUniqueLogTag(4432): doInBackground of WaitingForLoaderTask
  03-08 18:17:23.817: I/myUniqueLogTag(4432): waiting for Sync
  </code></pre>
  
  <p>and on </p>
  
  <pre><code>new LoaderTask(1000, 5).execute((Void[])null);
  new WaitingOnLoaderTask().execute((Void[])null);
  </code></pre>
  
  <p>i get a Log like this:</p>
  
  <pre><code>03-08 18:18:53.082: I/myUniqueLogTag(4621): sleeping for 5 times for 1000 milliseconds
  03-08 18:18:54.082: I/myUniqueLogTag(4621): sleeping for 4 times for 1000 milliseconds
  03-08 18:18:55.090: I/myUniqueLogTag(4621): sleeping for 3 times for 1000 milliseconds
  03-08 18:18:56.082: I/myUniqueLogTag(4621): sleeping for 2 times for 1000 milliseconds
  03-08 18:18:57.090: I/myUniqueLogTag(4621): sleeping for 1 times for 1000 milliseconds
  03-08 18:18:58.106: I/myUniqueLogTag(4621): sleeping for 0 times for 1000 milliseconds
  03-08 18:18:59.113: I/myUniqueLogTag(4621): doInBackground of WaitingForLoaderTask
  03-08 18:18:59.113: I/myUniqueLogTag(4621): setSyncFinish to notify all
  03-08 18:18:59.621: I/myUniqueLogTag(4621): waiting for Sync
  03-08 18:18:59.621: I/myUniqueLogTag(4621): finishedWaiting!
  </code></pre>
  
  <p>so I guess one is preventing the other to run. Did they compute their <code>doInBackground</code> actually in the same Thread, and <code>LOCK.wait()</code> blocks this Thread from executing further? </p>
  
  <p>Is there a way to fix it (and stay compatible to Android 2.2)?</p>
  
  <p>Note:
  the second Time is basically what I want, but in other Task implementations I cannot be shure that <code>WaitingOnLoaderTask</code> is only executed when the other one has finished. </p>