J2ME mutiplayer game loop

Posted on 2009-02-11
Last Modified: 2013-11-24
I'm designing multi player java mobile game. I'm just confused about how the game loop should be designed.

In simplest form there could be a infinite while() loop checking network input and some other events, GPS position, for instance, in each iteration. My question is how to avoid pooling information from threads in this while loop, if I have two threads, one taking care of input from network, another reading location information. If i make callback methods from these child threads to parent thread, they would each invoke separate callback method in parent thread.

I think having a loop pooling results from threads is not a good design, especially on resource limited mobile devices, but I'm not sure is there any other reasonable way in this case, it's my first game :)

For instance:

public class Game implements Runnable{

public void run{
//game should be controlled from here

public void updatePosition(byte[] location){
// location thread would call this independently from game loop

public void updateNetwork(byte[] message){
// network thread would call this independently from game loop


In this case I loose centralized control of game from while() loop, because of separate methods being called when new events occur.
What would be the best way how to structure the main game class?

Question by:python_
    LVL 7

    Expert Comment

    i'm not pretty sure how you meant by 'avoiding the infinite loop' because a game loop is indeed one, but there shouldn't be any problem if you update your game in the part where the function called affects, like for example using booleans to flag if the values from each update functions are ready for reading.

    Hope that helps,

    LVL 7

    Accepted Solution

    i haven't read the question well, sorry. i missed the 3rd paragraph there.

    well, mobile (and generally all video) games use the polling method. it does seem hard to take early on game development (because of the infinite loop idea) but you'll realize that this is the only efficient, if not the only, way to do this. building more threads would eat up more precious resources for the mobile phone than actually placing all of those threads in one loop, assuming that all of those threads sum up to at most 100ms (gives you 10 FPS) processing time.

    so yes, the most useful design for a game loop is actually the infinite while loop, that's why it was called a game loop in the first place.

    also, if you wanted to have different sleep times on each threads, you could simply compute for the elapsed millisecond time and update depending on the value, an example on the code snipplet (and a basic game loop).

    Hope that helps,


    // game loop
    long t = System.currentTimeMillis();
    while(!quit) {
        t = System.currentTimeMillis();
    // update function
    int countA=0, countB=0;
    void update(long msElapsed) {
        countA += (int)msElapsed;
        countB += (int)msElapsed;
        while(countA >= 500) { // if 500ms elapsed
            countA -= 500;
        while(countB >= 200) { // if 200ms elapsed
            countB -= 200;

    Open in new window


    Author Comment

    thanks :)

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    copyEvens challenge 6 47
    strDist challenge 35 71
    pairs challenge 5 33
    java. non-English characters encoding problem. intellij idea 3 26
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now