Java.lang.ref.reference<t>

Source: Internet
Author: User

//See the four types of references in Java before you start.
PackageCom.zby.ref;ImportSun.misc.Cleaner;/*** Abstract base class for referencing objects. This class defines the public operations for all referenced objects. Because reference objects are implemented in close collaboration with the garbage collector, this class cannot be directly inherited by the referenced object. * * @authorZhoubaiyun * *@param<T>*/ Public Abstract classReference<t> { /* * A reference instance is one of these four possible internal states. * Active Status: Subject to special handling of the garbage processor. Sometimes when the collector detects that an attainable reference object has changed to the appropriate state, the collector changes the status of the instance to suspended or inactive, * depending on whether the instance is registered when it is created. In the previous case, the collector also adds an instance to the pending reference list. The newly created instance makes the active state. * * Pending Status: An element in a pending reference list waiting to be queued by the referenced processing thread. An instance that is not registered cannot have this status. * Queued Status: An instance element within a queue that is registered at the time of creation. When an instance is removed from his own reference queue, it becomes inactive. An instance that is not registered cannot have this status. * Inactive: do nothing. An instance becomes inactive and it is impossible to change the state. * * status is encoded as below to queue and next fields: * * Active: Instance registration when queue = Referencequeue or if the instance is not registered Queue=referencequeue.null;next=null . * Hang: queue = The next instance in Referencequeue;next=queue when the instance is registered, if the instance is the last element in the queue, Next=this * * Queue: Queue = Referencequeue.en Queued;next=queue the next instance inside, if the instance is the last element of the queue, Next=this * is inactive: queue = Referencequeue.null; Next = this. * * With these constraints, the collector needs only to check the next field in order to determine whether a reference object needs to be special: if the next field is null the instance is active, and if * is not NULL, the collector should treat the instance normally. * * To ensure that the concurrent collector can discover active reference objects without interfering with the application thread that might invoke the queue () method on those objects, the collector should link the discovered objects through the discovered fields. */ PrivateT referent;//the object to which the GC treats specialreferencequeue<?SuperT>queue; Reference Next; Private transientReference<t> discovered;//VM Usage//objects used to be synchronized with the garbage collector. The collector must acquire this lock at the beginning of each collection cycle. //It is therefore critical that any code holding this lock must be completed as soon as possible, without allocating new objects and avoiding invoking user code. Private Static classLock {}; Private StaticLock lock =NewLock (); //the list of references waiting to be queued. When the reference processor thread removes the reference, the collector adds them to the list. This list is protected by the lock object above. Private StaticReference pending =NULL; //high-priority thread for queued pending references Private Static classReferencehandlerextendsThread {referencehandler (threadgroup g, String name) {Super(g, name); } Public voidrun () { for (;;) {Reference R; synchronized(lock) {if(Pending! =NULL) {R=pending; Reference RN=R.next; Pending= (Rn = = r)?NULL: RN; R.next=R; } Else { Try{lock.wait (); } Catch(Interruptedexception x) {}Continue; } } //Fast path for Cleaners if(rinstanceofCleaner) {((Cleaner) R). Clean (); Continue; } referencequeue Q=R.queue; if(Q! =referencequeue.null) Q.enqueue (R); } } } Static{threadgroup TG=Thread.CurrentThread (). Getthreadgroup (); //This is the top-level thread group, and it looks like it's a slip.//The main thread has a main threads group [Thread[main,5,main], NULL, NULL, NULL]//There is also a system thread group [thread[reference Handler,10,system], Thread[finalizer,8,system], thread[signal dispatcher,9, System], Thread[attach Listener,5,system]]. for(Threadgroup TGN =TG; TGN!=NULL; TG= TGN, TGN =tg.getparent ()); //start a reference processing threadThread handler =NewReferencehandler (TG, "Reference Handler"); //If there is a system-specific priority higher than the max_priority, then the highhandler.setpriority (thread.max_priority); Handler.setdaemon (true); Handler.start (); } /*accessors and settings for the object to be specially treated by the GC*/ /*** Returns the object to which the GC of the current reference object is to be treated, regardless of whether the object has been cleaned by the program or the garbage collector and returns NULL *@return */ PublicT Get () {return This. referent; } /*** Clean up this reference object, and executing this method will not cause the object to enter the queued queue. * This method will only be executed by Java code and will not be executed when the garbage collector performs cleanup. */ Public voidClear () { This. referent =NULL; } /*Query Operations*/ /*** Tell you if the reference object is a queue, whether it's a program or a garbage collector. If the reference object is not registered to the queue when it is created, this method returns FALSE. * @return */ Public Booleanisenqueued () {//as can be seen in the internal state, this method actually checks whether the instance is suspended or queued synchronized( This) { return( This. queue! = Referencequeue.null) && ( This. Next! =NULL); } } /*** Add this Reference object to any queue it is registered to. * This method will only be executed by Java code and will not be executed when the garbage collector performs cleanup. * @returnIf this reference object is successfully added to the queued queue, returns True if it has been added to the queued queue or is not registered to a queue when it is created to return false. */ Public BooleanEnqueue () {return This. Queue.enqueue ( This); } /*Construction Method*/Reference (T referent) { This(Referent,NULL); } Reference (T referent, Referencequeue<?SuperT>queue) { This. referent =referent; This. Queue = (Queue = =NULL) ?ReferenceQueue.NULL:queue; }}

After reading it, you understand that the JVM is running with at least a few threads open?

Java.lang.ref.reference<t>

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.