Skip to content

Instantly share code, notes, and snippets.

@dagezi
Last active September 14, 2017 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dagezi/d70a2d01f969e2e0d4269c01c149823f to your computer and use it in GitHub Desktop.
Save dagezi/d70a2d01f969e2e0d4269c01c149823f to your computer and use it in GitHub Desktop.
Create WeakReferece out of the inner class!
import java.lang.ref.WeakReference;
public class Www {
public static void main(String args[]) {
Runnable r0 = create0();
Runnable r1 = create1();
System.gc();
r0.run();
r1.run();
}
static Runnable create0() {
Www w = new Www();
WeakReference<Www> ref = new WeakReference<>(w);
return
new Runnable() {
public void run() {
System.out.println("0 " + ref.get());
}
};
}
static Runnable create1() {
Www w = new Www();
return
new Runnable() {
WeakReference<Www> ref = new WeakReference<>(w);
public void run() {
System.out.println("1 " + ref.get());
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment