Java Container-Reference classification and partial map usage

Source: Internet
Author: User

First, directory

1. Reference classification

2, understand Weakhashmap, Identityhashmap, Enummap

3. Synchronous control and read-only setting

Second, the code implementation

1. Reference Classification (interview)

Strong references (strongreference): References to Objects, GC runtime, are not recycled.

Weak reference (SoftReference): GC Runtime may be recycled when the JVM is out of memory.

Soft Reference (WeakReference): objects are recycled when the GC is running.

Virtual Reference (Phantomreference): Similar to non-silver, the main tracking object is recycled objects, cannot be used alone, to be used in conjunction with Referencequeue

Objective: To avoid the long-term presence of objects in memory and to solve the problem of garbage collection mechanism timing.

2, Weakhashmap (key if referenced, after recycling automatically delete Key-value objects)

 public  static  void   main (String [] args) { // 
   
     result: Map with size 2,new String () for soft objects is recycled  weakhashmap weakhashmap=
    new  
     Weakhashmap ();  
    // 
     "Key1", "Key2" exists in constant pool as strong objects are not recycled         weakhashmap.put ("Key1", "Hello" 
     "Key2", "Hello2" ); Weakhashmap.put ( new  String ("Key3"), "Hello3"        );        System.GC ();        System.runfinalization ();    System.out.println (Weakhashmap.size ()); }
   

3. Synchronous control and read-only operation

(1) Synchronization control

Because some of the containers are thread-safe and some are thread insecure, how can you adjust thread-insecure containers to thread-safe?

such as: Hashtale (SAFE)-hashmap (unsafe) arrylist-linkedlist (unsafe)-vector (security)

  

(2) Read-only operation

1) An immutable set of empty

  

2) An immutable set of elements

  

3) Non-variable container

  

Java Container-Reference classification and partial map usage

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.