4

I have a AsyncTask that runs and does its job and completes. onPostExecute does what its supposed to do and all is good. BUT the task is still running on the debug window in Eclipse. Every time I run the task I get a new one. So its not re-using the task I guess.

Has anyone seen this before?

Mark Worsnop
  • 4,407
  • 15
  • 54
  • 79

4 Answers4

1

It doesn't reuse the task. It keeps running until the onPostExecute finishes. It's probably still in your debug window because the object hasn't yet been garbage collected. Don't worry about it.

Falmarri
  • 47,727
  • 41
  • 151
  • 191
  • The last debug print completed in onPostExecute. the process finished. for the heck of it I tried running the process about 20 times and all 20 are still in the debug window. – Mark Worsnop Jan 05 '11 at 04:24
  • It's impossible to say without know exactly what you're doing and what you're seeing. But that doesn't sound unreasonable. – Falmarri Jan 05 '11 at 05:18
  • It just seems really weird to leave all the tasks running. When the app is loaded to an end user I will have no idea what is happening. – Mark Worsnop Jan 05 '11 at 13:38
  • Also I changed the code on one of the asynctasks to use a thread and that works, and doesnt hang. There doesn't seem to be as much flexibility using the threads however. I guess I need to write a hello world app that does a asynctask and see how that works, so nothing else is stopping it. – Mark Worsnop Jan 05 '11 at 13:40
  • Like I said, it's impossible to tell. If you post your code you're using in your asynctask we could probably help. Also how you're calling it. – Falmarri Jan 05 '11 at 16:00
  • So is the conclusion you don't have to worry about the AsyncTasks hanging around? – Eugene van der Merwe Mar 15 '11 at 20:01
  • 1
    I noticed the same behavior, AsyncTasks Threads are running even after they have finished doInBackground and onPostExecute. If I use threads instead of AsyncTask it is ok. About the garbage collector: how can thread that is running be collected? – peceps Jun 30 '11 at 13:56
1

Why must you "reuse the tasks"? The Javadoc for AsyncTask is pretty clear: we are not supposed to reuse an AsyncTask once doInBackground() has run to completion. Attempt to reuse will result in an exception.

The Javadoc's exact words: "The task can be executed only once (an exception will be thrown if a second execution is attempted.)"

Matt J.
  • 400
  • 1
  • 4
  • 15
  • I guess you didnt understand this. The task does not go away. it completes but it still hanging in the processes. I dont want to reuse it, I just want it to stop using the resources. I did further testing if you read on you will see that it makes 5 tasks, after 5 are made then it automatically resues the task. – Mark Worsnop Feb 16 '11 at 13:31
  • Actually - why can't we use it again ? I have a task that calls http server, and each time I want some data I want to use it. how can it be achieved otherwise ? – Dani Feb 19 '13 at 16:12
0

Any objects listed in the debugger won't be garbage collected. This is because the debugger holds a reference to them and is correct behaviour

Zulaxia
  • 2,702
  • 2
  • 22
  • 23
0

I wrote this test app to try the asynctask. it runs 5 background tasks, and then must reuse them. I would just like to know that this is OK as I have not been able to find any reference to this concern. I would like to know why 5 tasks? Do they release memory and whatever else they are taking? I would have put this in a comment but didnt seem to allow code snips

    package com.mvw.tas;

import java.io.IOException;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class TestAsync extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        Button btnD = (Button)findViewById(R.id.Btn01);

        btnD.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                new gback().execute( "");
            }
        });

    }

    ////////////////////////////////////////
    public class gback extends AsyncTask<String, Void, IOException> {
        //@Override
        protected IOException doInBackground(String... params) {
            Log.d( "ASTEST", "Running background");
            return null;

        }

        @Override
        protected void onPostExecute(IOException result) {
            Log.d( "ASTEST", "Post Exec");


        }

        @Override
        protected void onPreExecute() {

            Log.d( "ASTEST", "PRE Exec");;
        }

    }

}

and the XML

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />



<Button android:text="Test AysncTask" android:id="@+id/Btn01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
Mark Worsnop
  • 4,407
  • 15
  • 54
  • 79