Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 901825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:37:09+00:00 2026-05-15T15:37:09+00:00

I’m trying to write a game engine in Android, but I don’t have much

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:37:10+00:00Added an answer on May 15, 2026 at 3:37 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.