Last active
September 14, 2017 06:33
-
-
Save dagezi/d70a2d01f969e2e0d4269c01c149823f to your computer and use it in GitHub Desktop.
Create WeakReferece out of the inner class!
This file contains 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
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