Can I rely on the garbage collector to stop AsyncTask?

A quick theoretical question. Suppose I have a Java class that uses both a finalizer and an instance of its own private AsyncTask, not mentioned somewhere.

Now suppose the asyncTask doInBackground method looks something like this:

while(go) { f(); } 

and finalizer:

 public void finalize() { go = false; } 

When all external references to an object are deleted, will AsyncTask be stopped? Or will the system continue the stream without ever deleting the object, because the stream refers to it?

+4
source share
3 answers

Can I rely on the garbage collector to stop AsyncTask?

No, you can’t. In fact, you can rely on GC NOT to stop the task.

GC will only complete an object that has become inaccessible. But all active threads are achievable by definition, which means that the AsyncTask object AsyncTask also be available.

+2
source

The short answer is: the flow continues, you cannot rely on the GC to stop it.

Details: Unable to get a sufficient answer to my question (thank you, Alberto), I decided to test it empirically myself. Using the following test code:

 public class TestActivity extends Activity { private ThreadContainer mtc; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mtc = new ThreadContainer(); } public void btnFree_click(View view) { Log.v("TestActivity","Free clicked"); mtc = null; } } public class ThreadContainer { private boolean go = true; public ThreadContainer() { new testThread().execute(1); } private class testThread extends AsyncTask<Integer,Integer,Integer> { protected Integer doInBackground(Integer... arg0) { while(go) { Log.v("testThread","I'm running..."); try { Thread.sleep(500); } catch (InterruptedException e) { // do nothing } } return 0; } } @Override public void finalize() { go = false; } } 

I managed to get the following result from logcat:

 I/ActivityManager( 244): Displayed com.example.test/.TestActivity: +509ms V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/TestActivity(13728): Free clicked D/dalvikvm( 512): GC_EXPLICIT freed 144K, 50% free 2891K/5767K, external 1685K/2133K, paused 164ms V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... D/dalvikvm(13449): GC_EXPLICIT freed 12K, 47% free 2894K/5379K, external 1685K/2133K, paused 94ms V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... V/testThread(13728): I'm running... 

As you can see, the thread is not stopped, although its task is completely closed and there are no more external links to its container object.

+1
source

I think AsyncTask will never be stopped because you are doing this: go = false; , from the while . This loop will stop only when the go condition changes to false , within the loop. By executing the loop, as you wrote above, go conditon will never be updated, and the while loop will only check the initial state of the go variable. Take a look at this SO question.

right while loop example :

 class WhileDemo { public static void main(String[] args){ int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; } } } 

This will not be an infinite loop, because we are changing the while condition in the loop. You can also use the break statement to exit the loop correctly.

Another consideration. If AsyncTask marked as a daemon thread, when all external references to the object are deleted, the system must stop the thread. Read more about daemon flows here and here .

0
source

All Articles