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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:30:02+00:00 2026-05-26T16:30:02+00:00

I am working on a little Java game, leaning on the classic Asteroids. In

  • 0

I am working on a little Java game, leaning on the classic Asteroids.

In this game there are a ton of objects which need to be updated in certain time intervals:

Every frame: Objects that move or rotate.
Every second: The game timer.
Every few seconds: Enemy AI, enemy spawning
Every some-second: Firing a new bullet from the ship weapon.

Currently I’m handling it like this:

//This method is called by a timer that is run every 30 milliseconds
//It will iterate through all objects that require periodic updates and call their
//update method every 30ms.
public void frameUpdateLoop(){
    for( Updating u : allObjectsThatNeedPeriodicUpdates ){
        u.update();
    }
}

public class EnemySpawner implements Updating{
    private static final int SPAWN_TRESHOLD=500;
    private int timeUntilNewEnemySpawns=SPAWN_TRESHOLD;

    //This method is called by frameUpdateLoop()
    //It is only supposed to do work once every 500ms, but is called every
    //30ms nonetheless.
    public void update(){
        timeUntilNewEnemySpawns -= 30; //Subtract time since last update
        if( timeUntilNewEnemySpawns <= 0){
            SpawnNewEnemy();
            timeUntilNewEnemySpawns = SPAWN_TRESHOLD;
        }
    }
}

Of course this is only an example of how I am using it, so I removed the unnecessary parts.

My question is:
Is that the right (=a good way) of implementing such an update system?
When reading around I noticed that most of the time a Timer is used for such a task.

But that would require me to have dozens of timers running at once (one for each object that requires updating).
If I understand correctly each instance of a Timer also creates a new thread, so I fear that this could become a problem at some point (thread-handling performance loss outweights my current system – or the number of threads becomes too large).

I’m sort of new to Java so I don’t know if my fears are baseless or if it really is a bad idea to have so many Timers.

Thank you very much for suggestions, tips and corrections on this topic!

  • 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-26T16:30:02+00:00Added an answer on May 26, 2026 at 4:30 pm

    I really like your design with Updating interface abstracting the details of what actually has to be updated. Of course your current implementation isn’t very elegant. Maybe you should create several methods like updateEveryFrame(), updateEverySecond(), etc.? Each object implements only the methods that it needs to work, leaving others us no-ops.

    Timer is actually a good choice here as well:

    This class scales to large numbers of concurrently scheduled tasks (thousands should present no problem)

    However I would recommend splitting allObjectsThatNeedPeriodicUpdates into: allObjectsThatNeedUpdateEveryFrame, allObjectsThatNeedUpdateEverySecond, etc. Create one periodic task for first collection, another (with different interval) for the other.

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

Sidebar

Related Questions

This is the problem. I'm working on a little Java tool which is composed
I'm working on a little Java game in which all sorts of events can
I'm working on a class project to build a little Connect4 game in Java.
I've been working a little with DevExpress CodeRush and Refactor! Pro this week, and
I'm trying to get this piece of code working a little better. I suspect
I am working on a little pinball-game project for a hobby and am looking
First a little background about why I'm asking this question... I'm working on a
I've got a little game that a friend and I are writing in Java.
I'm working on a little server app with Java. So, I'm getting informations from
I'm working on a JavaScript collator/compositor implemented in Java. It works, but there has

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.