Four examples of Java reference source code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.