0

I want to optimize my code. I can see in my sample camera app, I am creating thread for takePicture. Something like:

WAY 1

private void takePicture() {
    mTakePictureThread = new Thread() {
        run() {
            camera.takePicture(cb, cb, ..);
        }
    }
    mTakePictureThread.start();
}

Now I can do same thing with Handler too as below:

WAY 2

//consider mTakePictureThread is started in onCreate()
//and mTakePictureHandler is global variable

private void takePicture() {
    mTakePictureHandler.sendMessage(1);
}

private class TakePictureThread extends Thread {
    @override
    public void run() {
        Looper.prepare();
        mTakePictureHandler = new Handler() {
            public void handlerMessage(Message msg) {
                int what = msg.what;
                switch(what) {
                    case 1:
                        camera.takePicture(...);
                    break;
                    default:
                    break;
                }
            }
        }
        Looper.loop();
    }
}

Considering takePicture is called many times. So in case 1, new thread will be created as many time as takePicture is called means every time new thread will be created.

But in second case, I can always hold one handler and call takePicture just by passing a message through handler.

So my query is, which one is better considering I am calling takePicture many time. In terms of performance and memory.

I have seen people using WAY 1 always(couldn't get satisfied reply why). So can anyone explain Pros and Cons of both approach and when should I follow which approach?

AndroDev
  • 3,236
  • 8
  • 35
  • 49

1 Answers1

1

The second way queries your messages and will only use one thread to take the pictures. So if your camera.takePicture(..) is not a blocking call this will result in unneccessary waiting times for your threads.

The first way can handle different query counts in the same time if your camera can broadcast the actual image.

You can find a good explanation of loopers in the answer on this question looper purpose. A looper is better if you want one thread to handle messages in a sequential manner.

Community
  • 1
  • 1
Weschne
  • 51
  • 3
  • As per camera documentation, next takePicture can be called only when last takePicture is completed and callbacks are notified. What my point of interest is if I use first way, every time I call takePicture, those many threads will be created and will increase runtime memory unless GC collects them. Also thread creation and starting it also a time consuming process. So still, will first way win? – AndroDev Feb 19 '16 at 11:04
  • [looper lifecycle] (http://prasanta-paul.blogspot.de/2013/09/android-looper-and-toast-from.html) this link gives a great overview of an looper lifecycle – Weschne Feb 19 '16 at 14:22
  • it seems that a looper needs to be reinitialised if the queue is empty so if you can assure that the queue never gets empty the looper should be faster then creating new threads over and over again. But way two is quite more code then way two so possible the easier error finding and maintainence leans people to use the first way. – Weschne Feb 19 '16 at 14:34