Code example for Semaphore

Methods: tryAcquire

0
        mOutput.close();
        super.tearDown(); 
    } 
 
    private void runOnLooper(final Runnable command) throws InterruptedException {
        final Semaphore sem = new Semaphore(0);
        mHandler.post(new Runnable() {
            @Override 
            public void run() { 
                try { 
                    command.run();
                } finally { 
                    sem.release();
                } 
            } 
        }); 
        if (! sem.tryAcquire(WAIT_TIMEOUT, TimeUnit.MILLISECONDS)) {
            fail("Failed to run the command on the looper.");
        } 
    } 
 
    private final class CameraErrorCallback implements android.hardware.Camera.ErrorCallback {