Use of the thinking in Java Reference class

Source: Internet
Author: User

Reference is a special reference class in Java. Describes a reference to a special effect, primarily on garbage collection objects.

It has 3 sub-classes:

1.SoftReference;

2.WeakReference

3.PhantomReference

First look at the instance of thinking in Java

 Packagecontainers;Importjava.lang.ref.PhantomReference;Importjava.lang.ref.Reference;ImportJava.lang.ref.ReferenceQueue;Importjava.lang.ref.SoftReference;Importjava.lang.ref.WeakReference;Importjava.util.LinkedList;//description and encapsulation of references, reference classclassverybig2{Private Static Final intSize= 10000; Private Long[] La=New Long[SIZE]; PrivateString ident;  PublicVERYBIG2 (String ID) {ident=ID; } @Override PublicString toString () {returnident; }    protected voidFinalize () {System.out.println ("Finalizing" +ident); }} Public classReferencestest {Private StaticReferencequeue<verybig2> rq=NewReferencequeue<verybig2>();  Public Static voidCheckqueue () {Reference<?extendsVerybig2> inq=Rq.poll (); if(Inq! =NULL) {System.out.println ("In queue:" +inq.get ()); }    }         Public Static voidMain (string[] args) {intSize= 10; if(args.length> 0) {size=NewInteger (args[0]); } LinkedList<SoftReference<VeryBig2>> sa=NewLinkedlist<softreference<verybig2>>();  for(inti = 0; i < size; i++) {Sa.add (NewSoftreference<verybig2> (NewVeryBig2 ("Soft" +i), RQ); System.out.println ("Just Created:" +sa.getlast ());        Checkqueue (); } LinkedList<WeakReference<VeryBig2>> wa=NewLinkedlist<weakreference<verybig2>>();  for(inti = 0; i < size; i++) {Wa.add (NewWeakreference<verybig2> (NewVeryBig2 ("Weak" +i), RQ); System.out.println ("Just Created:" +wa.getlast ());        Checkqueue (); } softreference<VeryBig2> s=NewSoftreference<verybig2> (NewVeryBig2 ("Soft")); WeakReference<VeryBig2> w=NewWeakreference<verybig2> (NewVeryBig2 ("Weak")); System.GC ();//performs a garbage collection operation on objects that are not referenced. Also, the Finalize method that calls the reclaimed objectLinkedList<PhantomReference<VeryBig2>> pa=NewLinkedlist<phantomreference<verybig2>>();  for(inti = 0; i < size; i++) {Pa.add (NewPhantomreference<verybig2> (NewVeryBig2 ("Phantom" +i), RQ); System.out.println ("Just Created:" +pa.getlast ());        Checkqueue (); }    }}

Output

Just created: [Email protected]
Just created: [Email protected]
Just created: [Email protected]
Just created: [Email protected]
Just created: [Email protected]
Just created: [Email protected]
Just Created:java.lang.ref.so[email protected]
Just created: [Email protected]
Just created: [Email protected]
Just created: [Email protected]
Just created: [Email protected]
Just created: [Email protected]
Just created: [Email protected]
Just created: [Email protected]
Just created: [Email protected]
Just created: [Email protected]
Just created: [Email protected]
Just created: [Email protected]
Just created: [Email protected]
Just created: [Email protected]
Just created: [Email protected]
In Queue:null
Just created: [Email protected]
In Queue:null
Finalizing Weak
Just created: [Email protected]
In Queue:null
Just created: [Email protected]
In Queue:null
Just created: [Email protected]
In Queue:null
Just created: [Email protected]
In Queue:null
Just created: [Email protected]
In Queue:null
Just created: [Email protected]
In Queue:null
Finalizing Weak 9
Just created: [Email protected]
In Queue:null
Finalizing Weak 8
Finalizing Weak 7
Just created: [Email protected]
In Queue:null
Finalizing Weak 6
Finalizing Weak 5
Finalizing Weak 4
Finalizing Weak 3

From the results, it can be seen that:

1. Garbage collector priority Level:softreference< WeakReference < phantomreference.

2. When the garbage collection method System.GC is run, the target object's Finalize method is called first.

3.

Use of the thinking in Java Reference class

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.