3
\$\begingroup\$

I am studying the gameloop used in Replica Island to understand a bit about Android game development. The main loop has the below logic...

GameThread.java (note the variable names in sample code dont match exact source code)

 while (!finished) {

        if (objectManager != null) {

            renderer.waitDrawingComplete();
            //Do more gameloop stuff after drawing is complete...
      }
 }

I was curious to see what waitDrawingComplete actually does, but found its just an empty syncrhonized method! I guess it does nothing, am I missing something here?

GameRenderer.java line 328

 public synchronized void waitDrawingComplete() {
 }

Source code can be checked out here with SVN if you feel like having a look: https://code.google.com/p/replicaisland/source/checkout

\$\endgroup\$
3
\$\begingroup\$

The source code gives a good clue:

/**
 * This function blocks while drawFrame() is in progress, and may be used by other threads to
 * determine when drawing is occurring.
 */

public synchronized void waitDrawingComplete() {

Then if we look in onDrawFrame we see

synchronized (this) {
  // big block of drawing code
}

The synchronized keyword usage here means that one of the methods will block until the other function is complete (onDrawFrame or waitDrawingComplete). In other words the empty function is simply used to wait until the drawing is complete, so it does do something.

A sequence of events might look like this:

  • Thread 1: code execution is inside onDrawFrame
  • Thread 2: waitDrawingComplete is called. This thread blocks because code is inside the synchronized in Thread 1.
  • Thread 1: Reaches the end of onDrawFrame
  • Thread 2: waitDrawingComplete executes and returns and execution returns to the following code.
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I'm digging through the source and cannot find any drawFrame() method. Any clue where I may find it? \$\endgroup\$ – Shijima May 29 '14 at 20:08
  • \$\begingroup\$ I think it's a typo and they were actually referring to onDrawFrame. \$\endgroup\$ – CiscoIPPhone May 29 '14 at 20:10
  • \$\begingroup\$ just realized that after I posted... I guess the piece I'm not understanding is, how does that empty method tie in with onDrawFrame, is there some callback I'm not seeing or something? \$\endgroup\$ – Shijima May 29 '14 at 20:12
  • \$\begingroup\$ It's all about the synchronized keyword on waitDrawingComplete and the synchronized(this) in onDrawFrame. That's what ties them together, neither one can enter those synchronized parts of code until the other thread/s have exited the synchronized parts of code. \$\endgroup\$ – CiscoIPPhone May 29 '14 at 20:17
  • \$\begingroup\$ Understood, thanks for the explanation sir. Now that we have established the point of that empty method, do you think this is a good approach to concurrent programming? I'm new to game concurrency and this seems like a confusing way to code? \$\endgroup\$ – Shijima May 29 '14 at 20:21

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.