Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Async task slowing down my UI Thread

I am new to Android Application development and was having a problem with Async tasks. So I'm trying to create an ECG graphing application that does some background processing while the graphing is happening.

I've defined the following Async task -

private class Erosion extends AsyncTask <Void,Void,Void> {


    @Override
    protected Void doInBackground(Void...unused ) {

        int i,tempIndex;
        double[] tempArray = new double[13];
        double min = ecgSamples[ecgSampleForErosionIndex] - gArray[0]; 
        while (ecgIncoming)
        {
            if (ecgSampleForErosionIndex > 179999)
            {
                ecgSampleForErosionIndex = 0; 
            }

            for(i= 0;i<13;i++)
            {
                tempIndex = ecgSampleForErosionIndex + i; 
                if (tempIndex > 179999)
                {
                    tempIndex =  (ecgSampleForErosionIndex + i) - 180000;
                }
                tempArray[i] = ecgSamples[tempIndex] - gArray[i];
                if (tempArray[i] < min)
                {
                    min = tempArray[i];
                }

            }

            //min needs to be stored in the erosionFirst Array

            if (erosionFirstArrayIndex > 179999)
            {
                erosionFirstArrayIndex = 0; 
            }


            ecgErosion[erosionFirstArrayIndex] = min; 
            erosionFirstArrayIndex++;
            ecgSampleForErosionIndex++;


        }
        return null;


    }

    } //End of Async Task  

So all I'm trying to do is modify the content of a particular array in the async task - i dont need to update the UI (at least not for now)

However, when I run this async task my ECG graphing slows down and becomes jerky. When I comment out the "new Erosion().execute();" part in the code where I start the Async task, the graphing becomes normal again.

Isn't an async task supposed to be on a separate thread and so not affecting how things are happening on my UI thread? What am I doing wrong?

like image 988
stu90 Avatar asked Mar 09 '12 18:03

stu90


1 Answers

Even if you run a heavy piece of code on a background thread it will still affect the CPU load of the device and therefore might cause delays in your UI thread as well, specially if the device has single-core CPU.

It seems like you have a very heavy loop in you doInBackground method, which runs constantly and just use the CPU nonstop, which overloads it. I'm not sure what this loop is for, but if it doesn't have to refresh constantly you might want to consider adding a thread sleep, allowing other threads to get more CPU time :

    while (ecgIncoming)
    {
      ... do your thing ...
      Thread.sleep(100); // wait for 100 milliseconds before running another loop
    }

Obviously the "100" is just a number, if the array can update once a second, make it a 1000, etc...

like image 185
Talihawk Avatar answered Oct 01 '22 20:10

Talihawk