Short answer: the thread keeps going, you can't rely on the GC to stop it.
Details: Not being able to get a sufficient answer to my question (thank you however, Alberto) I decided to test this out empirically by 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 was able to get the following output 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, even though its task is completely private and there are no longer any external references to its container object.