Skip to main content
gamedev.net
🔒 Locked

Threads problem in Java ME

Started by Tom21 2 replies 550+ views
Original Post
Tom21
Tom21
Hello, I have create a simple java ME shooting game. since the game consists of player, enemy, environment and etc, I use multiple threads to run all this at the same time. When I run the program, the program run slowly in the sense of player movement, enemy movement and the environment movement is very slow. If I use lesser threads, the program move faster than multiple threads. So, if too many threads run at the same time, will it influence the response time of the program and make it slow? Is there any way to program the player, enemy and environment in a single thread?
markr
markr
Using multiple threads to move multiple objects is not a common technique and is generally discouraged.

Most J2ME applications (i.e. games) have exactly two threads, one (or perhaps more) which is created by the platform which calls event handlers. This thread does nothing other than setting status flags.

Then another thread which runs a timer, perhaps extending TimerTask. This then carries out any logic and rendering operations, before calling serviceRepaints(). I cannot stress how important it is for *ALL* logic and rendering to be carried out in this thread.

If you don't do it this way, it's unlikely to work smoothly; this is how J2ME is designed to work.

Mark
Tom21
Tom21
Thanks for your advice and I will try to improve it.
Tom21
Tom21
Hello Mark,
I have reduced the number of threads. But, now I faced another problem, which is the "OutOfMemoryError". My game has 3 stages. For example, after the first stage, I would like to clear the memory buffer like the heap memory, then only load the second stage. Otherwise, the memory may not enough when the stages increased. So, is there any way to clear the buffer for garbage collection? I try to assign all class object as null, but the problem still there.

In my game, it has a menu. The menu has the option like "New Game", "Help", "Exit". Whenever the game is start, the menu will add-in another option - "Continue", which will allows user back to the game and continue it.

Everytime when I select "New Game", the game will start running, and when I press for menu, the game will pause automatically and then show the menu list.
In this case, the game is still in the memory, but in paused state. If I choose "New Game" from the menu, it means it will restart a new game.

My problem is that when I restart a new game, the heap memory still keeping the previous game. How could I delete the previous game's memory, so that whenever the new game is selected, the heap memory that store previous game would be cleared and the memory buffer will always store the current game only?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.