Code example for CountDownLatch

Methods: await

0
    if (Looper.myLooper() == Looper.getMainLooper()) {
      // On main thread already, Just Do It™. 
      drawDecorViewToBitmap(activity, bitmap);
    } else { 
      // On a background thread, post to main. 
      final CountDownLatch latch = new CountDownLatch(1);
      activity.runOnUiThread(new Runnable() {
        @Override public void run() { 
          try { 
            drawDecorViewToBitmap(activity, bitmap);
          } finally { 
            latch.countDown();
          } 
        } 
      }); 
      try { 
        latch.await();
      } catch (InterruptedException e) {
        String msg = "Unable to get screenshot " + file.getAbsolutePath();
        Log.e(TAG, msg, e);
        throw new RuntimeException(msg, e);
      }