Skip to content

Instantly share code, notes, and snippets.

@anitaa1990
Created May 16, 2018 17:17
Show Gist options
  • Save anitaa1990/21faa5702d6fabac665fe459ddfedf60 to your computer and use it in GitHub Desktop.
Save anitaa1990/21faa5702d6fabac665fe459ddfedf60 to your computer and use it in GitHub Desktop.
public class ThreadReferenceLeakActivity extends AppCompatActivity {
/*
* FIX I: make variable non static
* */
private LeakyThread leakyThread = new LeakyThread();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
createThread();
redirectToNewScreen();
}
private void createThread() {
leakyThread.start();
}
private void redirectToNewScreen() {
startActivity(new Intent(this, SecondActivity.class));
}
@Override
protected void onDestroy() {
super.onDestroy();
// FIX II: kill the thread
leakyThread.interrupt();
}
/*
* Fix III: Make thread static
* */
private static class LeakyThread extends Thread {
@Override
public void run() {
while (!isInterrupted()) {
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment