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