Code example for CountDownLatch

Methods: await

0
        } 
    } 
 
 
    void beforeCall() throws Exception { 
        final CountDownLatch latch = new CountDownLatch(1);
        final Exception[] exceptions = new Exception[1];
 
        // Execute onSuccess in the UI thread, but wait 
        // for it to complete. 
        // If it throws an exception, capture that exception 
        // and rethrow it later. 
        handler.post(new Runnable() {
            public void run() { 
                try { 
                    new Callable() {
                        @Override 
                        public Object call() throws Exception {
                            doOnPreCall(); 
                            return null; 
                        } 
                    }.call(); 
                } catch (Exception e) {
                    exceptions[0] = e;
                } finally { 
                    latch.countDown();
                } 
            } 
        }); 
 
        // Wait for onSuccess to finish 
        latch.await();
 
        if (exceptions[0] != null)
            throw exceptions[0];
 
    }