Java--Weakhashmap

Source: Internet
Author: User

First, Introduction

Reference types in Java are made up of four cases, strong references, soft references, weak references, and virtual references.                                                                                                                                              An introduction to these can be found in my next blog post. Http://www.cnblogs.com/plxx/p/4217178.html

Ii. Overview

Weakhashmap, and HashMap are peers in the family.

public class weakhashmap<k,v>  extends abstractmap<k,v>  implements Map<k,v>

For Weakhashmap---an entry in a weakhashmap would automatically be removed when it key is no longer in ordinary U Se. When key is no longer in use, it is automatically cleared. What is the status of no longer used??? when GC root is not enumerated, it is assumed that this can be referred to by GC (finalizable-->finalized-->reclaimed).

Like HashMap, Weakhashmap allows <key,value> to be null, with an initial size of 16,load factor of 0.75, not thread-safe.


In a Weakhashmap

Once the key is cleared, it can no longer be created, queried, and later entry removed.

Each key object in a  weakhashmap  
Value objects don't strongly refer to their own keys, either directly or indirectly,
Since that'll prevent the keys from being discarded

The key is stored in weak reference. While value is stored in the strong reference,value is not known directly or indirectly associated with the key, otherwise it will prevent the removal of key.

Third, view the source code

1  Private voidexpungestaleentries () {2          for(Object x; (x = Queue.poll ()) = =NULL; ) {3             synchronized(queue) { //Add Queue Lock control 4@SuppressWarnings ("Unchecked")5Entry<k,v> e = (entry<k,v>) x;6                 inti =indexfor (E.hash, table.length);7 8Entry<k,v> prev =Table[i]; //Find the bucket number where the entity is located 9Entry<k,v> p =prev;
            //Find this entity
Ten while(P! =NULL) { OneEntry<k,v> next =P.next; A if(p = =e) { - if(prev = =e) -Table[i] =Next; the Else -Prev.next =Next; - //must not null out e.next; - //Stale entries May is in use by a hashiterator +E.value =NULL;//Help GC -size--; + Break; A }
               //Remove the entity-unrecoverable
atPrev =p; -p =Next; - } - } - } -}

Expunge-stale-entries Erase stale Entities

The method is called in the related operation of resize (), size (), Gettables (). Erase objects that are not in use.

Java--Weakhashmap

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.