Four examples of Java reference source code
Do not explain, directly on the code, the words cannot top a line of code.
PackageCom.apkkids.javalanguage;ImportJava.lang.ref.PhantomReference;ImportJava.lang.ref.Reference;ImportJava.lang.ref.ReferenceQueue;ImportJava.lang.ref.SoftReference;ImportJava.lang.ref.WeakReference;ImportJava.lang.reflect.Field;/** * @author WXB * Java Four references * Strong references: Objects with strong references are not garbage collected at any time * Soft references: objects that have soft references are garbage collected when memory is low: objects with weak references are When rows are reclaimed * Virtual references: Objects that have only virtual references are the same as no references, except that they are garbage collected when they are placed in the associated reference queue and can be observed * * / Public class fourreferencetest { /** * @param args * * Public Static void Main(string[] args) {//Three Tests, please comment out the other two when testing onePhatomreferencetest ();//Weakreferencetest ();//Softreferencetest ();}Static voidPhatomreferencetest () {String s =NewString ("ABC");//Create a reference queue RQ, and then create a virtual reference with RQ and S FinalReferencequeue<string> RQ =NewReferencequeue<string> (); Phantomreference<string> WR =NewPhantomreference<string> (S,RQ);//wr.get () will always return nulls =NULL;//Create a thread loop to detect if s joins the queue NewThread () {BooleanBflag =true;@Override Public void Run() {Object obj =NULL; while(Bflag) {obj = Rq.poll ();//rq in the end will poll out a phantomreference if(obj! =NULL) {bflag =false;Try{The reason for using reflection is that the get method of phantomreference always returns null //Even if the upward transition to reference is not possible, only the referent stored in the reference class can be directly reflected through reflectionField f = Reference.class.getDeclaredField ("Referent"); F.setaccessible (true); string s = (string) f.get (obj); System.out.println ("Get String from Phantomreference:"+s); }Catch(Nosuchfieldexception e) {E.printstacktrace (); }Catch(SecurityException e) {E.printstacktrace (); }Catch(IllegalArgumentException e) {E.printstacktrace (); }Catch(Illegalaccessexception e) {E.printstacktrace (); }}}}}.start ();Try{//The main thread sleeps after running GC, waiting for child threads to runThread.Sleep ( +); System.GC (); Thread.Sleep ( +); }Catch(Interruptedexception e) {E.printstacktrace (); } }Static voidWeakreferencetest () {String s =NewString ("ABC"); Weakreference<string> WR =NewWeakreference<string> (s); s =NULL; while(Wr.get ()! =NULL) {System.out.println ("WeakReference get:"+wr.get ()); System.GC (); System.out.println ("System.GC ()"); } }/** * Not running out of memory the test will not exit * / Static voidSoftreferencetest () {String s =NewString ("ABC"); Softreference<string> WR =NewSoftreference<string> (s); s =NULL; while(Wr.get ()! =NULL) {System.out.println ("SoftReference get:"+wr.get ()); System.GC (); System.out.println ("System.GC ()"); } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Four examples of Java reference source code