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

  • SEARCH
  • Home
  • 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 7595501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:41:14+00:00 2026-05-30T21:41:14+00:00

I am developing an Android game and have a strange issue where occasionally the

  • 0

I am developing an Android game and have a strange issue where occasionally the game will become unresponsive for a long period of time before springing back into life. As far as I can tell, this pause, if it occurs at all, only happens when the game starts up. Once running normally the game seems to behave itself.

After some investigation, it appears that the onTouchEvent callback is becoming blocked trying to acquire a lock on the synchronization object it shares with the game thread. Meanwhile the game thread is running normally, and not holding onto a lock on the synchronization object for any long period of time.

It was my understanding that the synchronized block in doTouchEvent would acquire a lock on mSyncObject as soon as that lock had been released by the game thread. But in some cases, it appears that the game thread is able to acquire and release a lock several hundred times before doTouchEvent is eventually able to acquire its lock.

There is no other code that uses the same object for synchronization.

I’ve copied the relevant bits of code below. From what I can gather, it’s not doing anything out of the ordinary, so I am somewhat baffled by the strange behaviour that I am seeing.

Would appreciate any help on this one. Thanks in advance!

class GameThread extends Thread {

// ...some methods and members omitted...

private volatile int mFrameCount = 0; 
private Object mSyncObject = new Object();

@Override
public void run() {
    while (!mShutDown) {
        Canvas canvas = null;
        long timestampA = System.currentTimeMillis();
        try {
            synchronized (mSurfaceHolder) {
                canvas = mSurfaceHolder.lockCanvas(null);
                // Synchronized on our object...
                synchronized (mSyncObject) {
                    long now = System.currentTimeMillis();
                    if ((now > mLastTime) && !mPaused) {
                        double timestep = (double) (now - mLastTime) / 1000.0;
                        mGame.update(timestep);
                    }
                    mLastTime = now;
                    if (canvas != null) {
                        mGame.draw(canvas);
                    }
                }
            }
        } finally {
            if (canvas != null) {
                mSurfaceHolder.unlockCanvasAndPost(canvas);
            }
        }

        // have tried inserting a sleep() here, but it didn’t help

        ++mFrameCount;
    }
}


// Called from the UI thread
public boolean doTouchEvent(MotionEvent event) {
    boolean result = false;

    // Synchronized on our object... 
    // The game loop in run() acquires and releases a lock
    // on this object on every frame, so would expect to be
    // blocked here for no longer than one frame.
    // However, on occasions have been blocked here for over
    // 2000 iterations of the game loop. 
    int frameCount = mFrameCount;
    synchronized (mSyncObject) {
        int framesWaited = mFrameCount - frameCount;
        if (framesWaited > 1) {
            Log.i("Block", "doTouchEvent waited " + framesWaited + " frames for lock");
        }

        if (!(mPaused || mShutDown)) {
            result = mGame.doTouchEvent(event);
        }
    }
    return result;
}

}
  • 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-30T21:41:15+00:00Added an answer on May 30, 2026 at 9:41 pm

    It would appear that this issue is down to the fact that synchronization offers no guarantee of fairness when it comes to the order in which waiting threads will acquire a lock. In fact, a thread can be kept waiting indefinitely for a lock if another cpu-intensive thread is repeatedly acquiring and releasing locks.

    See this thread discussing exactly the same issue…

    http://groups.google.com/group/android-developers/browse_frm/thread/ffe76e4a433c8675/f424fb7dc3baeb10

    …and here for an example of a thread-safe but synchronization-free solution.

    http://blog.tomgibara.com/post/208684592/avoiding-starvation

    Before stumbling upon the above, I changed my code to pass motion events between threads using a ConcurrentLinkedQueue, which also seemed effective in eliminating the stalls.

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

Sidebar

Related Questions

I'm developing a crime themed android game that will have a high maturity rating
I am developing an android game, in which i have some trains moving on
I'm currently developing a simple 2D game for Android. I have a stationary object
I am a new game developer and have started developing game for android. I
I am developing an android game application,I have implemented all the screens.Now i want
good day we are developing game for android and have some trouble with resource
I'm developing an android app, and have developed a game, but I now have
I am developing a game for Android and have what I imagine to be
I'm currently developing a game for android. I have added buttons to allow the
I' developing a small android maze game and I'm experiencing a strange effect which

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.