java four kinds of references and callbacks

JAVA four kinds of reference

Java object reference include:

  1. Strong references
  2. Soft references
  3. Weak references
  4. False quote

Java provides four types of reference has two main purposes:
  the first is to allow programmers to determine the life cycle of certain objects through code;
  the second is in favor of JVM garbage collection.
1. A strong reference
  means to create an object and assign a reference to the object variable.
  For example:
  Object Object = new new Object ();
  String str = "ha ha ha";
  strong references will never be garbage collected, JVM would rather throw OutOfMemory error does not recover this object has a reference variable points.
2. Soft references
  Soft references feature is that if the JVM memory space is full, the garbage collector does not reclaim the soft reference variables.
  () Method returns a Java object in front of the collector get the Java object recovery, SoftReference classes provide strong reference. In addition, once the garbage recycling thread Java objects, get () method will return null.
  For chestnut:
  the BinarySearch the BinarySearch new new BS = (); // binary search
  bs.find (Test, 0, test.length -. 1,. 5);
  the SoftReference <the BinarySearch> ruanYinYong the SoftReference new new = <the BinarySearch> (BS);
  BS = null;
  ruanYinYong.get () Find (Test, 0, test.length -. 1,. 5);. // even stronger then refer to the null, but also can continue to use soft references.
  If there is a strong reference to the same time associated with it, then it will not be recovered when the object is garbage collection. (Too weak references)
3. Weak references
  WeakReference <People> Reference new new WeakReference = <People> (new new People ( "zhouqian", 20 is));
  System.out.println (reference.get ());
  System.gc ( ); // notify garbage collected resources
  System.out.println (reference.get ());
  a first output, a second output a null
reference 4. virtual

  java PhantomReference used to represent a virtual reference. Just as there is no reference to an object associated with the same, if an object is associated with a virtual reference, then at any time JVM could be recovered off. Virtual reference can not be used alone, it must be used together with the reference queue.

  When the garbage collector and the virtual object is found associated with a reference, it will prior to recovery, this reference is added to the reference virtual queue. Determining whether the program can reference has been added to the virtual queue references to see if the referenced object is to be recovered, thereby performing the operation.

  ReferenceQueue<String> queue = new ReferenceQueue<>();
  PhantomReference<String> str = new PhantomReference<String>("chx", queue);
  System.out.println(str.get());

The callback function using soft incorporated by reference

import java.lang.ref.SoftReference;

public class CallBackMain {
SoftReference<CallBackTest> ruanYinYong = new SoftReference<CallBackTest>(new CallBackTest());

public static void main(String[] args) {
new CallBackMain().startCallBack();
}

void startCallBack public () {
System.out.println ( "I do anything else");
ruanYinYong.get () callBack ();.
}
}

CallBackTest {class
public static int = B. 1;

public void callBack () {
System.out.println ( "I is the callback function" B +);
}
}

Guess you like

Origin www.cnblogs.com/chxwkx/p/11225873.html