I’m working on my first game, and it works fine, but clearly needs some optimizing. Initially, I had everything running on one thread (I used the LunarLander API to get started on gaming). Since then, I’ve added 2 new threads to the game. It still works just fine, but I see no improvement. I must be doing something wrong..
This is the basic setup:
class GameView extends SurfaceView implements SurfaceHolder.Callback {
private Mover mover;
private Collider collider;
private GameThread thread;
class Mover extends Thread{
//methods
}
class Collider extends Thread{
//methods
}
class GameThread extends Thread{
//methods
}
public GameView(Context context, AttributeSet attrs) {
mover = new Mover();
collider = new Collider();
thread = new GameThread();
thread.start();
}
}
Then, in the GameThread class I call methods on other threads when needed:
collider.checkCollision();
Is this the proper way to reduce load on the main thread?
Well, until they bring out dual-core phones, multiple threads isn’t going to buy you anything in terms of performance improvements.
Multi threading can help when you’re doing I/O intensive stuff on the additional threads (because they’ll spend most of their time blocked on I/O, allowing your main thread to continue processing). But when you’ve got CPU-heavy stuff like collision-detection happening then you’re just going to have those two thread fighting for the one CPU core.