2

So I am running some threads with a CountDownLatch.

My problem is that when I call latch.await() the UI seems to hang and even UI commands that were called beforehand have no effect. e.g.

btnShare.setVisibility(View.INVISIBLE);
prgSharing.setVisibility(View.VISIBILE);

latch.await()

The first two lines have no effect on the UI.

Any idea why this is and possibly a solution? Thanks.

jim
  • 8,670
  • 15
  • 78
  • 149

2 Answers2

3

This is most likely because you are blocking the UI-Thread before it can render the Views again. I think you should look at AsyncTask and maybe put your wait logic in the doInBackground() method, or somehow re-think your implementation.

Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
  • 1
    Good idea, in my UI Thread I am processing the multiple web tasks but offload this to a AsyncTask where I can then await() for the two to finish and pass the final result back to the UI Thread. Thanks. – jim Oct 22 '12 at 14:56
2

if the UI hangs it is because you call:

latch.await()

on the UI Thread. You have to avoid blocking call on the UI Thread since those are the cause of ANR

Blackbelt
  • 156,034
  • 29
  • 297
  • 305