Code example for Lock

Methods: tryLock, unlock

0
    } 
 
    @Override 
    protected void onDraw(Canvas canvas) {
 
        if (cameraPreview == null || cameraPreviewLock.tryLock() == false)
            return; 
        else 
            try { 
                if (canvasRect == null)
                    canvasRect = canvas.getClipBounds();
 
                /* draw the canvas onto the screen */ 
                canvas.drawBitmap(bitmap, cameraRect, canvasRect, null);
                canvas.drawText(String.valueOf(framesPerSecond), 0, textSize, frameRateText);
 
                if (captureNextFrame) {
                    captureNextFrame = false;
                    PictureHandler.savePicture(getContext(), bitmap);
                } 
 
                final long now = System.currentTimeMillis();
                if (now - time > 1000) {
                    framesPerSecond = frames;
                    frames = 0;
                    time = now;
                } else { 
                    frames++;
                } 
 
            } finally { 
                cameraPreviewLock.unlock();
            } 
    } 
 
    public void onPreviewFrame(byte[] yuv, Camera camera) {
        if (cameraPreviewLock.tryLock() && yuv != null) {