I'm working on an android app which at some point runs a service. That service runs a new thread for a time-consuming operation and waits for a countDownLatch to be zero. After that it runs a second thread for another time-consuming operation (that new thread isn't really necessary now but will be in the near future).
Now...after starting the downloadThread, if I do the waiting as shown below, the main thread AND the downloadThread suspend operations. However, if I wait for the countDownLatch inside the uploadThread everything works fine.
Thread downloadThread = new Thread(new Runnable() {
@Override
public void run() {
downloadFiles(mMediaFileList);
}
});
downloadThread.start();
try {
Log.d("UploadMedia", "Waiting for DownloadMedia to finish");
mCountDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread uploadThread = new Thread(new Runnable() {
@Override
public void run() {
uploadMedia();
}
});
uploadThread.start();
It's not really a problem but isn't it supposed to work both ways? Is there something I'm missing about the .await() functionality?
Cheers!
Edit: FYI, downloadFiles has callbacks which wait for completion or failure of the task. Can it be that those callbacks work on the main thread which is being suspended by the .await()?