當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java java.lang.ref.SoftReference用法及代碼示例

當我們在Java中創建一個對象時,默認情況下對象不是軟的。要創建軟引用對象,我們必須將其顯式指定為JVM。在軟引用中,即使對象可以進行垃圾回收,但直到 JVM 嚴重需要內存時,它也不會被垃圾回收。當 JVM 內存不足時,對象將從內存中清除。

為什麽使用軟引用對象?

當創建軟引用對象時,它會被標記為垃圾回收。但是,除非 JVM 內存不足,否則不會對它進行垃圾回收。

java.lang.ref.SoftReference Class in Java

SoftReference 類中的構造函數:

Constructor parameters

Constructor Description

SoftReference(T 指代) 創建引用給定對象的新軟引用。
SoftReference(T 所指對象,ReferenceQueue <T> q) 創建一個新的軟引用,該引用引用給定的對象並注冊到給定的隊列中。

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<MyClass> 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 ...");
    }
}
輸出
-> 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 <X> softobj = new SoftReference <X> (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

從參考類繼承的方法:

方法名稱

Method Description

清除 () 清除該引用對象。
入隊 () 將此引用對象添加到其注冊的隊列(如果有)。
得到 () 返回此引用對象的指示對象。
已入隊 () 告知此引用對象是否已被程序或垃圾Collector排隊。


相關用法


注:本文由純淨天空篩選整理自varunkedia大神的英文原創作品 java.lang.ref.SoftReference Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。