简体   繁体   中英

ConcurrentLinkedQueue usage between main thread and AsyncTask in Android

I have ConcurrentLinkedQueue and Queue is updated in main thread and AsyncTask display the data inside the ConcurrentLinkedQueue . It works ok in the first launch.

If the application is stopped by back key or home key, then the application is relaunch, the problem is there. In main thread, the Queue has data, but in AsyncTask , Queue length is zero.

However, the application is forced stopped and relaunched, then it is fine.

What could be the issue with this Queue ?

I use Fragment.

 public class OneFragment extends Fragment implements SensorEventListener {
    Queue<Float> queuex = new ConcurrentLinkedQueue<Float>();
    Queue<Float> queuey = new ConcurrentLinkedQueue<Float>();
    Queue<Float> queuez = new ConcurrentLinkedQueue<Float>();

    public void onCreate(Bundle savedInstanceState) {
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    }
    @Override
    public void onSensorChanged(SensorEvent event) {
       queuex.add(Float.valueOf(deltaX));
       queuey.add(Float.valueOf(deltaY));
       queuez.add(Float.valueOf(deltaZ));

    }

    protected class Update extends AsyncTask<Context, Integer, String> {
    float x_,y,z;
    @Override
    protected String doInBackground(Context... params) {

        int i = 0;
        while (true) {
            try {
                if(!queuez.isEmpty()) { //Always zero after relaunch if the app is stopped by pressing home or back buttons.

                    Float g_x = queuex.poll();
                    x_ = g_x.floatValue();
                    Float g_y = queuey.poll();
                    y = g_y.floatValue();
                    Float g_z = queuez.poll();
                    z = g_z.floatValue();
                    publishProgress(i);
                    i++;
                    if(systemON == true)
                        mVibrator.vibrate(1000);

                }


            } catch (Exception e) {

            }
        }
        //return "COMPLETE!";
    }

    // -- gets called just before thread begins
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }


    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

        mSeriesX.add(x, x_);
        mSeriesY.add(x, y);
        mSeriesZ.add(x++, z);
        if(x%50 == 0) {
            mRenderer.setXAxisMin(x);
            mRenderer.setXAxisMax(x+50);
        }
        if (mChartView != null) {
            mChartView.repaint();
        }
    }

    // -- called if the cancel button is pressed
    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
    }
   }
 }

First off, you should never have an infinite loop in an AsyncTask. Use a Thread instead. In fact if your task will take longer than a few seconds you should use a Thread, because multiple tasks can't execute concurrently (default implementation is to share a single task thread).

Secondly- your problem is due to the fact the task runs forever. That means the task for the previous activity (remember back finishes the activity) is still running when your new Activity launches. So your old task is still running, and it has access to the old instance of Activity- thus the old instance of the queue. The new instance of the activity is writing to the new instance of the queue, but the old task can't see it.

You should move from an AsyncTask to a Thread so that you don't have the single task problem. You should also interrupt the thread when your activity is destroyed, and have the loop in the thread check isInterrupted to see if the thread is interrupted, and if so exit the loop to kill the thread as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM