Code example for Semaphore

Methods: tryAcquire

                // This method runs in the service's main thread (and there's no way 
                // to move it to a child thread, since it needs to return an answer 
                // to the GfxEngine), and when the keyguard blocks here, Android has 
                // problems. So it's better to add a timeout to the block waiting. 
                if (!mEventBufferSemaphore.tryAcquire(10, TimeUnit.SECONDS)) {
                    // The GfxEngine is not currently expecting this exception and it will 
                    // try to use the null pointer. We should probably fix this in the GfxEngine, 
                    // but a short term solution is to return "" instead of null. 
                    return ""; 
            } catch (InterruptedException e) {
                // return null; 
                return ""; 
            /* remove the next event from the queue and return it */ 
            if (mEventBuffer.isEmpty()) {
                return "";