Android game engine design: how to synchronize the game loop and canvas update thread?


I wanted to rewrite my simple game engine to run on Android and I am wondering how can I synchronize two running threads. Right now I have the following:

  • Runner is the main activity, entry point for this game;
  • CanvasView is just a canvas on which drawing is being made;
  • GameWorld is - as the name suggests - class which stores the current information about the state of the game. For now, lets just say that it also contains a Level.
  • GameLoop is a separate thread which is updating the game state;
  • CanvasThread is a separate thread, which is being run to draw the current Level on CanvasView.

As the level is just a simple array, CanvasThread just iterates through the array and draws it on screen. And I have few questions regarding this:

  • is there a possibility to run the onDraw() method of CanvasThread on demand? In current state of the engine it is just being relaunched when the execution of previous one is finished.
  • I want to implement some kind of three way handshake between GameLoop and CanvasThread, something similar to:

    1. GameLoop -> CanvasThread: please stop updating
    2. Canvas -> GameLoop: ok, stopped
    3. GameLoop -> CanvasThread: ok, you may resume.

What is the best way to do so? I am a total Java / Android newbie so my way of setting the engine is most probably not the best / optimal one. If you have any suggestions to the design, I will gladly appreciate them.

Thank you.

PS: If I had violated all best practices while creating the diagram above, please forgive me.

Android has an easy way to handle thread communication. You can use a Looper and a Handler to send messages, or complete Runnables, between the Threads.

Look at Android Guts: Intro to Loopers and Handlers on for an introduction.

You can send an "empty" message to signal something, or supply some arguments. You can send the message immediately, or with some delay.

If you design your game loop by sending messages to itself, it would be easy to inject messages from other threads.