views:

149

answers:

2

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?

+3  A: 

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.

Dean Harding
And even if you have two CPU's, making collision detection rely on other threads is a sure-fire way of making sure that only one CPU is effectively used.
Arafangion
@Arafangion: yes, good point, your threads all need to be as decoupled as possible in order to maximize parallelism.
Dean Harding
So what I'm hearing is I've effectively done nothing, and I should look into other ways to optimize android scripts like the one Lirik pointed out below?
Snailer
@Snailer, the ideas I mentioned will help you **if** you start multithreading, but as others have pointed out- *in your case* there is no need for you to run multiple threads since it will not increase performance.
Lirik
A: 

Aside from what others have already pointed out, I would also like to mention a couple of other concepts that should be kept in mind when you're multithreading in general:

  1. Don't extend the Thread class- give it a Runnable! (from Jon Skeet himself)
  2. Take advantage of the ExecutorService when staring threads. (here is why)

You will probably find the most benefit from multithreading if you have some combination of the following (just off the top of my head):

  1. You have more than one core!
  2. You have tasks that can run independently of each other.
  3. You need to keep some portion of your app responsive (i.e. GUI), while another portion does some work (this is one of those exception cases where you may do multithreading on a single core).
  4. You can easily determine the main points of contention and you can "eliminate" them in a reasonable way (lock-free, wait-free, or with some synchronization).
Lirik