📜  Java的.lang.ref.SoftReference类在Java中

📅  最后修改于: 2022-05-13 01:54:39.180000             🧑  作者: Mango

Java的.lang.ref.SoftReference类在Java中

当我们在Java创建对象时,默认情况下对象不是软的。要创建软引用对象,我们必须向JVM显式指定它。在软引用中,即使对象可用于垃圾回收,它也不会被垃圾回收,直到 JVM 严重需要内存。当 JVM 内存不足时,对象会从内存中清除。

为什么使用软引用对象?

当一个软引用对象被创建时,它被标记为垃圾收集。但是,除非 JVM 内存不足,否则它不会被垃圾回收。

Java 中的 java.lang.ref.SoftReference 类

SoftReference 类中的构造函数:



Constructor parameters

Constructor Description

SoftReference ( T referent)Creates a new soft reference that refers to the given object.
SoftReference ( T referent, ReferenceQueue q)Creates a new soft reference that refers to the given object and is registered with the given queue.

get() 方法:

Java
// Java program to show the demonstration
// of get() method of SoftReference Class
 
import java.lang.ref.SoftReference;
class GFG {
    public static void main (String[] args) {
       
        // creating a strong object of MyClass
          MyClass obj = new MyClass ();
       
          // creating a weak reference of type MyClass
          SoftReference sobj = new SoftReference<>(obj);
       
          System.out.println ("-> Calling Display Function using strong object:");
          obj.Display ();   
       
          System.out.println ("-> Object set to null");
       
          obj = null;
       
        // Calling the get() method
          obj = sobj.get();
          System.out.println ("-> Calling Display Function after retrieving from soft Object");
          obj.Display ();
    }
}
class MyClass {
      void Display ()
    {
        System.out.println ("Display Function invoked ...");
    }
}


Java
// Java program demonstrating all the methods
// of SoftRefernce Class
 
import java.lang.ref.SoftReference;
class GFG {
    public static void main (String [] args) {
      
        // Creating object of Class X
        X obj = new X ();
       
        // Creating a soft reference of type X
          SoftReference  softobj = new SoftReference  (obj);
       
          System.out.println ("-> Retrieving object from Soft Reference using get ()");
          softobj.get().show();
       
          System.out.println ("-> Is it possible to queue object using enqueue ()");
          System.out.println (softobj.enqueue ());
       
          System.out.println ("-> Checking if reference is queued using isEnqueued ()");
          System.out.println (softobj.isEnqueued ());
    }
}
class X {
    void show()
    {
        System.out.println ("show () from X invoked..");
    }
}


输出
-> Calling Display Function using strong object:
Display Function invoked ...
-> Object set to null
-> Calling Display Function after retrieving from soft Object
Display Function invoked ...

示例显示了 SoftReference 类的 enqueue() 和 isEnqueued() 方法:

Java

// Java program demonstrating all the methods
// of SoftRefernce Class
 
import java.lang.ref.SoftReference;
class GFG {
    public static void main (String [] args) {
      
        // Creating object of Class X
        X obj = new X ();
       
        // Creating a soft reference of type X
          SoftReference  softobj = new SoftReference  (obj);
       
          System.out.println ("-> Retrieving object from Soft Reference using get ()");
          softobj.get().show();
       
          System.out.println ("-> Is it possible to queue object using enqueue ()");
          System.out.println (softobj.enqueue ());
       
          System.out.println ("-> Checking if reference is queued using isEnqueued ()");
          System.out.println (softobj.isEnqueued ());
    }
}
class X {
    void show()
    {
        System.out.println ("show () from X invoked..");
    }
}
输出
-> Retrieving object from Soft Reference using get ()
show () from X invoked..
-> Is it possible to queue object using enqueue ()
false
-> Checking if reference is queued using isEnqueued ()
false
输出
-> Retreiving object from Soft Reference using get ()
show () from X invoked..
-> Is it possible to queue object using enqueue ()
false
-> Checking if reference is queued using isEnqueued ()
false

从引用类继承的方法:

Method Name

Method Description

clear ()Clears this reference object.
enqueue ()Adds this reference object to the queue with which it is registered, if any.
get ()Returns this reference object’s referent.
isEqueued ()Tells whether this reference object has been enqueued, either by the program or by the garbage collector.