I need to keep running a thread which does image processing using the frames from the camera of an android device.

I've tried the simplest way

```
new Thread() {

    @Override
    public void run() {
        while (true) {
            /* Processing frames here */
        }
    }
}.start();

```

Buts this hogs the CPU too much and the UI starts lagging. Also, added `Thread.sleep(300)` after processing the frame so that the UI thread can get some CPU time but although it does help to some extent, it doesn't feel like the right way to do it.

I would like to have some ideas about a good approach to handle this.

**EDIT:** Using `AsyncTask`

```
private DetectionTask detectionTask;

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
    /* Doing some stuff here */

    camera.addCallbackBuffer(data);

    if (detectionTask != null && detectionTask.getStatus() != AsyncTask.Status.FINISHED)
        return;

    detectionTask = new DetectionTask();
    detectionTask.execute();
}

private class DetectionTask extends AsyncTask<Void, Void, float[]> {

    @Override
    protected float[] doInBackground(Void... params) {
        /* Processing frames here */
        return result;
    }

    @Override
    protected void onPostExecute(float[] result) {
        if (result != null) {
           /* Update UI */
        }
    };
};

```