Code example for Semaphore

Methods: acquire, release

0
     */ 
    public boolean sendToast(final String text) {
        Long now = new Long(SystemClock.uptimeMillis());
 
        try { 
            mSem.acquire();
        } catch (InterruptedException e) {
            return false; 
        } 
 
        try { 
            Long when = mPreviousToast.get(text);
 
            if (null != when) {
                if ((now - when) < mDelayTimeMs) {
                    // If we've recently sent this message, don't send it again. 
                    Log.d(TOAST_TAG, "skipped: " + text + " now: " + now + " when: " + when);
                    return false; 
                } 
                Log.d(TOAST_TAG, "using: " + text);
            } else { 
                if (mPreviousToast.size() > mDepth) {
                    // We could implement LRU, but instead, just do a random 
                    // replace as we shouldn't hit the limit that often. 
                    String[] s = (String[]) mPreviousToast.keySet().toArray();
                    int pick = (int) (mRand.nextFloat() * mDepth);
                    mPreviousToast.remove(s[pick]);
                    Log.d(TOAST_TAG, "removed: " + s[pick] + "size: " + mPreviousToast.size());
                } 
            } 
            mPreviousToast.put(text, now);
        } finally { 
            mSem.release();
        } 
 
        final Context ctx = mContext;
        Runnable r = new Runnable() {
            public void run() {