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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:22:01+00:00 2026-06-02T10:22:01+00:00

I am working on a game using the thread-per-client model. The game operates so

  • 0

I am working on a game using the thread-per-client model. The game operates so that every half a second all of the players need to be updated. The updates all have to happen within the half a second interval, so they need to all be executed at the same time. My idea is to have a class that keeps track of when the “global update” needs to happen and then when the time comes, go through all of the players and tell it to update:

 for(Player p : currentPlayers) {
     p.update();
 }

The problem is that since every player has their own thread, I want the player to take advantage of that fact since it is already there. If I go through the updates from a different class, then there is one thread executing what a couple hundred threads could be doing individually. How can I design it so that if a thread calls method ‘update()’ in class Player, a different thread than the one that called it executes it, or perhaps a different approach that can achieve the same goal?

If you need any clarification, please ask! Thanks a lot!

UPDATE: What do you guys think of this (I can’t figure out how to format long amounts of code in posts!):

UPDATE 2: Just realized I would need to know when all of the players finish updating to reset the last time…

public class PseudoExample implements Runnable
{
    // Assume 'players' is full of active players.
    private Player[] players = new Player[100];
    private long lastUpdate = System.currentTimeMillis();

    public void run()
    {
        while (true)
        {
            if (System.currentTimeMillis() - lastUpdate >= 500)
            {
                for (Player p : players)
                {
                    p.update = true;
                }
            }

            try
            {
                Thread.sleep(10);
            } catch (InterruptedException e)
            {

            }
        }
    }

    private class Player implements Runnable
    {
        private boolean update = false;

        public void run()
        {
            while (true)
            {
                if (update)
                {
                    // Do updating here ...
                }

                try
                {
                    Thread.sleep(10);
                } catch (InterruptedException 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. Editorial Team
    Editorial Team
    2026-06-02T10:22:03+00:00Added an answer on June 2, 2026 at 10:22 am

    I think the best way to handle this would be instead of calling p.update(), you could send an asynchronous message to p. This would use the Handler functionality. This is probably the cleanest way, although I believe some (likely trivial) overhead will occur from the message passing.

    So, in your ticking thread (i.e. the one that calls the global update), you would have a reference to a Handler object for each client thread. Then, you look would look like

    for (Player p : currentPlayers) {
        p.handler().sendMessage(); // this isn't exactly the syntax
    }
    

    and in your Player, you would have a PlayerHandler object that extends Handler and overrides handleMessage(Message).

    EDIT: the comments on the question are good ones – don’t use more threads than you need to. They might seem to be the “right” abstraction, but they introduce a ton of potentially tricky issues. If all of your computation needs to be done in between ticks, it might not matter whether it’s done sequentially or not.

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

Sidebar

Related Questions

I'm working on a C project that consists of a simple game using the
I'm developing a game, that is running in a separate thread. Now I need
I am working on a game using Corona sdk. I need to determine a
My situation is that I am working on a Canvas game using Box2DWeb, so
I am working on a 2D iPhone game using OpenGL ES and I keep
I am currently working on designing my first FPS game using JOGL. (Java bindings
I am working on an authentication system for an online game programmed using PHP
I'm working on a GUI for a card game and am using the ACM's
I'm working on a sudoko solver (python). my method is using a game tree
I am working on a Facebook game which is developed using Zend framework. Right

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.