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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:24:00+00:00 2026-05-11T12:24:00+00:00

I wrote a simple applet to have as a base fr making games, and

  • 0

I wrote a simple applet to have as a base fr making games, and it’s already using up >50% of my CPU. I’m on a 3ghz P4 with 1.5gb ram, so I know it shouldn’t take up THAT much.

import java.awt.*; import java.applet.*;  public class applettest extends Applet implements Runnable {      long lastFrame;      public void init() {          (new Thread(this)).start();     }      public void paint(Graphics g) {         g.drawString('Welcome to Java!!', 50, 60 );      }      public void run() {          while(true) {             // code here             repaint();              try{                 // wait 16 milliseconds to cap frame rate to 60 fps                 while (System.nanoTime() < lastFrame + 160000000)       {                     Thread.yield();                 }                  lastFrame = System.nanoTime();             }              catch(Exception e){}         }     } } 
  • 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. 2026-05-11T12:24:01+00:00Added an answer on May 11, 2026 at 12:24 pm

    Try replacing your busy wait with

    lastFrame = System.currentTimeMillis();  while(true)  {    repaint();     long msSleep = 16 - (System.currentTimeMillis() - lastFrame);    lastFrame = System.currentTimeMillis();     if(nsToSleep > 0)    {       Thread.sleep(msSleep);    }    else    {       Thread.yield();  // Only necessary if you want to guarantee that                        // the thread yields the CPU between every frame    } } 

    This will guarantee that the time between frames is at least 16ms. If your render time is less than 16ms, and there aren’t any other threads hogging the CPU, it will run at 60fps.

    Your original solution will enforce the 16ms minimum, but it has to keep polling the system time over and over (which uses the CPU) until the necessary amount of time has passed.

    Notes:

    • repaint() is an asynchronous call (i.e. it will return immediately)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote not so simple app, and decided to make applet. I have the
I wrote a simple plugin which sets some css code using wp_options. It all
I have to integrate a Java applet into a simple asp.net 2.0 control. This
I have a simple 2d strategy combat game that I wrote in javascript and
I want to write a simple applet to put in a tray. Let's say
i wrote simple 3 functions to scrape titles , description and keywords of simple
I wrote simple class that on the start it just increase the value of
I wrote simple load testing tool for testing performance of Java modules. One problem
I wrote a simple XML file and a DTD file including an entity, but
I wrote a simple function, that makes emacs add matching quotes (so when 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.