When writing a game loop from scratch in java for an android game applications, it is very common in many tutorials I have read that the keyword synchronized is used to synchronize update and rendering methods of the SurfaceHolder of the SurfaceView that manages the game objects.

I understand the basics of synchronization and the importance of synchronizing i.e. a method which should be called n times, and is called inside multiple threads. But what I want to know is if the synchronization block, that synchronizes around a SurfaceHolder, and contains the SurfaceHolder's draw (rendering) and update methods, needs the synchronization if the plan is to ONLY call the update and draw methods inside one single thread (the game loop).

To conclude, the main concerns I have are:

1) Can the draw method inside the SurfaceView class be called by some inherrited, or just hidden method that I'm not aware of, so that I always have to synchronize the method?

2) Is there any reason to NOT use the synchronization if it's not needed; i.e., is the functionality computationally heavy?

Please let me know if anything in my question is unclear :)


This post assumes the game uses a “core gameplay loop” and concurrent threads for gameplay and rendering.

Usually game engines render one frame behind the core gameplay loop, using the most up to date state (position, rotation, color, etc) of all the game objects from the previous cycle. Besides disregarding synchronous operation entirely and risking many race conditions by trying to render during a game loop, this is the lowest-latency way to do rendering.

The synchronization needed here is to make sure that:

  1. The next cycle state is ready before the render thread starts on it, so the same cycle state does not get rendered twice.
  2. The render thread finishes frame n before frame n+2 is started by the gameplay loop, which frees up CPU power for the rest of the render task and stops the gameplay loop from getting more than a single cycle ahead from the render.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.