什么是这个无名的功能?

时间:2010-08-24 20:51:44

标签: android

以下代码示例中的这一行是什么意思?

synchronized (_surfaceHolder) {
    _panel.onDraw(c);
}

我可以猜到它的作用,但它叫什么,它是如何工作的?它是无名的同步函数吗?

class TutorialThread extends Thread {
    private SurfaceHolder _surfaceHolder;
    private Panel _panel;
    private boolean _run = false;

    public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) {
        _surfaceHolder = surfaceHolder;
        _panel = panel;
    }

    public void setRunning(boolean run) {
        _run = run;
    }

    @Override
    public void run() {
        Canvas c;
        while (_run) {
            c = null;
            try {
                c = _surfaceHolder.lockCanvas(null);
                synchronized (_surfaceHolder) {
                    _panel.onDraw(c);
                }
            } finally {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (c != null) {
                    _surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

那里没有隐藏方法,代码只是在_surfaceHolder对象上进行同步。基本上,它表示在执行{}之前的行之前锁定_surfaceHolder。

请参阅Intrinsic Locks and Synchronization