Created
May 16, 2018 17:17
-
-
Save anitaa1990/21faa5702d6fabac665fe459ddfedf60 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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