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) {
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.
source share