views:

112

answers:

1

I'm trying to write a game engine in Android, but I don't have much familiarity with threads.

My thread has an attribute, mSurfaceHolder, which holds the surface that I'll be drawing to. The run() method for my thread looks like this:

public void run() {
    // We're running now
    setState(STATE_RUNNING);

    // Keep looping while the game is running or paused
    while (mState == STATE_RUNNING || mState == STATE_PAUSED) {
        synchronized (mSurfaceHolder) {
            // If the game is running, update physics etc.
            if (mState == STATE_RUNNING) {
                updateGame();
            }
            // Draw the game even if it's paused
            drawGame();
        }
    }
}

STATE_RUNNING represents the state when the activity is in the foreground and the game should be running. STATE_PAUSED represents the state when another activity has come into the foreground. I'm not completely sure why I need to still draw while it's paused, but that's what I seem to have gathered from the LunarLander example.

What I'm hoping is that while I'm looking at the activity, the game will update and draw (which I test by using LogCat). And then when I go back to the home screen or another activity appears over the top, it will just draw.

Well it does draw and update while I'm watching the activity, so the game loop itself works. But when I leave the activity, it has no effect. Here is the thread's pause() method that is called from the activity's onPause():

public void pause() {
    Log.d("Game","Here");
    synchronized (mSurfaceHolder) {
        Log.d("Game","There");
        // If the thread is running, pause it
        if (mState == STATE_RUNNING) {
            setState(STATE_PAUSED);
        }
    }
}

As you can see, to test this method I have logged some messages. Now what I find when I leave the activity is that "Here" is logged, but "There" is not. Now with my limited knowledge of threads (I hardly know what synchronized actually does), I believe this will happen because my thread can't get synchronized with the surface holder. But I don't know WHY it doesn't synchronize. A few seconds after I've left the activity, I see the following warning in LogCat:

Activity pause timeout for HistoryRecord

Any idea why this would happen? There are no problems if I try to start the activity again, the thread just keeps running as it was.

Thanks very mucho.

EDIT: Just discovered something else. The thread pauses just fine if I leave the activity within about a second of having started it. And then it will resume and pause again with no problems at all while the same task is still running. I have no idea why for a short period of time it will work, but if I leave it too long, it won't.

EDIT 2: Okay... I fixed it. But I don't think I'm supposed to do what I've done. I've basically removed any synchronization with mSurfaceHolder from both the pause() and the setState() methods (which is used by pause()). No it works as it's supposed to, but I'm thinking the synchronization is there for a reason.

Perhaps the best question for me to ask is this: WHEN should you synchronize a thread with an object by use of a synchronized block? And in this case, what is the purpose of synchronizing with the SurfaceHolder?

+2  A: 

The synchronize keyword in Java makes sure that two threads cannot touch the same object at the same time, preventing race conditions. In other words, it implements locking.

Most likely, you're encountering deadlock here. Another thread is probably using mSurfaceHolder, and is therefore holding the lock for this object. Your code in synchronized (mSurfaceHolder) will block until that other thread completes, which apparently isn't happening.

However, since you're not actually modifying mSurfaceHolder in any way, there's no reason to hold a lock on it. The only thing you have to keep in mind is that mState might get read between the time pause() gets called and you update it's value. If that's a problem, then synchronize on mState.

Trevor Johns
Thanks, that really helped me understand this situation. I did a bit of reading around the subject too.
sixfoottallrabbit