[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


J2ME mutiplayer game loop

Posted on 2009-02-11
Medium Priority
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_
  • 2

Expert Comment

ID: 23612977
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,


Accepted Solution

gam3r_3xtr3m3 earned 750 total points
ID: 23613646
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

ID: 23620595
thanks :)

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month20 days, 2 hours left to enroll

872 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