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 8708655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:11:18+00:00 2026-06-13T04:11:18+00:00

I am currently trying to make a little game for Android. I try to

  • 0

I am currently trying to make a little game for Android.
I try to measure the time between two actions of the game but the begin time stays 0 when I try to call the currentTimeMillis() method after I called the gameThread.run() method (gameThread is a runnable).
When I place it before the run() call, it delivers the right value.

Class Gameview:

@Override
public void surfaceCreated(SurfaceHolder holder)
{
    // at this point the surface is created and we start the gameloop
    gameThread.setRunning(true);
    gameThread.run();
    begin=System.currentTimeMillis();
}

/**
 * This is the game update method. It iterates through all the objects and
 * calls their update method
 */
public void update()
{
    for (GuiElement element : guiElements)
    {
        element.update();
    }
    count++;
    if (ball.getPosY() >= Statics.DISPLAY_HEIGTH - ball.getRadius())
    {
        Log.d(TAG, "Displayhoehe : " + Statics.DISPLAY_HEIGTH);
        Log.d(TAG, "DisplayfactorHeight : " + Statics.DISPLAY_FACTOR_HEIGHT);
        Log.d(TAG, "speedvalue : " + ball.getSpeedY());
        Log.d(TAG, "ballradius : " + ball.getRadius());
        Log.d(TAG, "am boden mit " + count + " schritten");
        gameThread.setRunning(false);
        end = System.currentTimeMillis();
        Log.d(TAG, "begin, end " + begin + " " + end);
        Log.d(TAG, "time " + (end - begin));
    }
}

Class GameThread (implements runnable)

public void run()
{
    Canvas canvas;
    Log.d(TAG, "Starting game loop");

    long beginTime; // the time when the cycle begun
    long timeDiff; // the time it took for the cycle to execute
    int sleepTime; // ms to sleep (<0 if we're behind)
    int framesSkipped; // number of frames being skipped

    while (running)
    {
        canvas = null;
        // try locking the canvas for exclusive pixel editing in the surface
        try
        {
            canvas = surfaceHolder.lockCanvas();

            synchronized (surfaceHolder)
            {
                beginTime = System.currentTimeMillis();
                framesSkipped = 0; // resetting the frames skipped
                // update game state
                gameView.update();
                // draw state to the screen
                gameView.draw(canvas);
                // calculate how long did the cycle take
                timeDiff = System.currentTimeMillis() - beginTime;
                // calculate sleep time
                sleepTime = (int) (FRAME_PERIOD - timeDiff);

                if (sleepTime > 0)
                {
                    // if sleepTime > 0 we're OK
                    try
                    {
                        // send the thread to sleep for a short period very
                        // (useful for battery saving)
                        Thread.sleep(sleepTime);
                    }
                    catch (InterruptedException e)
                    {
                    }
                }

                while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS)
                {
                    // we need to catch up (skip some draw() calls)
                    this.gameView.update(); // update without rendering
                    sleepTime += FRAME_PERIOD; // add frame period to check
                                                // if we catched up
                    framesSkipped++;
                }
            }
        }
        finally
        {
            // in case of an exception the surface is not left in an inconsistent state
            if (canvas != null)
            {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }
}

EDIT: i tried to Log the return value of currentMillis() at the beginning point and it seems that the method gets called after the run() method has finished. Shouldn’t it run synchronous?

@Override
public void surfaceCreated(SurfaceHolder holder)
{
    // at this point the surface is created and we start the gameloop
    gameThread.setRunning(true);
    gameThread.run();
    begin=System.currentTimeMillis();
    Log.d(TAG, "currentMillis at beginning " + System.currentTimeMillis());
}

Log:

10-21 19:43:17.480: D/GameView(19767): Displayhoehe : 480
10-21 19:43:17.480: D/GameView(19767): DisplayfactorHeight : 1.0
10-21 19:43:17.480: D/GameView(19767): speedvalue : 1.6666666666666667
10-21 19:43:17.480: D/GameView(19767): ballradius : 20
10-21 19:43:17.480: D/GameView(19767): am boden mit 133 schritten
10-21 19:43:17.480: D/GameView(19767): begin, end 0 1350841397483
10-21 19:43:17.480: D/GameView(19767): time 1350841397483
10-21 19:43:17.480: D/GameView(19767): currentMillis at beginning 1350841397484

Solution: I started the Runnable incorrectly. That’s the right way to start the Runnable:

gameThread.setRunning(true);
Thread t = new Thread(gameThread);
t.start();
begin=System.currentTimeMillis();
  • 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-06-13T04:11:19+00:00Added an answer on June 13, 2026 at 4:11 am

    That’s because you’re calling the run() method directly instead of calling the Thread’s start() method.

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

Sidebar

Related Questions

The graph I'm currently trying to make falls a little between two stools. I
So, I was trying to make game in allegro but I'm currently stuck with
I'm working on a Zappa app, and I'm currently trying to make a little
I'm making a little toy command window with Tk, and currently trying to make
So what I'm trying to make is a little space game, You control the
I'm currently trying to make a little Web App to be used along with
I'm currently trying to make a set of conversion functions which, through one call,
I'm currently trying to make an orientation calculator in java and I'm having a
I am currently trying to make a sectioned table like this: Section 1: Entry
I am currently trying to make a dropbox-esque application from a tutorial and I

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.