0

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()?

1 Answer 1

0

It's not evident where your latch gets count down. But it sounds as it happens during uploadMedia(). If so, your main thread gets stale at latch.await() because there is no other controlflow that could count the latch down to zero.

1
  • Thanks for your input! Actually, the latch gets counted down inside downloadMedia. When the latch reaches zero it means all files have been downloaded. uploadMedia just grabs all available files and uploads them. The methods which implement callbacks inside downloadFiles are from a third party library...that's why I'm not sure as to where are those callbacks "sent" to. Sep 28, 2017 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.