I read about some methods of playing Android for training.
I came across some code that synchronizes access to the SurfaceHolder object used in the game loop thread, why is this necessary?
Example code found in sdk / samples / android-18 / legacy / LunarLander, LunarView.LunarThread class:
public void run() { while (mRun) { Canvas c = null; try { c = mSurfaceHolder.lockCanvas(null); synchronized (mSurfaceHolder) { if (mMode == STATE_RUNNING) updatePhysics(); synchronized (mRunLock) { if (mRun) doDraw(c); } } } finally { if (c != null) { mSurfaceHolder.unlockCanvasAndPost(c); } } } }
Thus, this piece of code accesses the canvas from the mSurfaceHolder object, which then locks while drawing the canvas. Why do I need synchronization?
I think this is not the case, since the only thread requesting the canvas and drawn on it is the thread in which the game loop is running.
Also , the documnetation document of SurfaceHolder.lockCanvas () says that it will use the internal lock until unlockCanvasAndPost () is called, then why is there additional synchronization?
source share