Code example for ReentrantLock

Methods: lock, sleep, unlock

0
    private final Canvas internalLockCanvas(Rect dirty) {
        if (mType == SURFACE_TYPE_PUSH_BUFFERS) {
            throw new BadSurfaceTypeException(
                    "Surface type is SURFACE_TYPE_PUSH_BUFFERS"); 
        } 
        mSurfaceLock.lock();
 
        if (DEBUG) Log.i(TAG, "Locking canvas..,");
 
        Canvas c = null;
        if (onAllowLockCanvas()) { 
            if (dirty == null) {
                if (mTmpDirty == null) {
                    mTmpDirty = new Rect();
                } 
                mTmpDirty.set(mSurfaceFrame);
                dirty = mTmpDirty;
            } 
 
            try { 
                c = mSurface.lockCanvas(dirty);
            } catch (Exception e) {
                Log.e(TAG, "Exception locking surface", e);
            } 
        } 
 
        if (DEBUG) Log.i(TAG, "Returned canvas: " + c);
        if (c != null) {
            mLastLockTime = SystemClock.uptimeMillis();
            return c;
        } 
         
        // If the Surface is not ready to be drawn, then return null, 
        // but throttle calls to this function so it isn't called more 
        // than every 100ms. 
        long now = SystemClock.uptimeMillis();
        long nextTime = mLastLockTime + 100;
        if (nextTime > now) {
            try { 
                Thread.sleep(nextTime-now);
            } catch (InterruptedException e) {
            } 
            now = SystemClock.uptimeMillis();
        } 
        mLastLockTime = now;
        mSurfaceLock.unlock();
         
        return null; 
    } 
 
    public void unlockCanvasAndPost(Canvas canvas) {