📜  Java的.lang.ref.WeakReference类在Java

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

Java的.lang.ref.WeakReference类在Java

当我们在Java创建一个对象时,默认情况下一个对象并不弱。要创建弱引用对象,我们必须向 JVM 显式指定它。

为什么使用弱引用对象:

与 C/C++ 不同, Java支持动态垃圾回收。这是在 JVM 运行垃圾收集器时执行的。为了不浪费空间,垃圾收集器会删除所有无法访问的对象。然而,为了标记一个对象进行垃圾回收,我们可以创建一个弱引用对象。


Java
// Java Program to show the usage of
// java.lang.ref.WeakReference Class
import java.lang.ref.WeakReference;
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
        WeakReference wobj
            = new WeakReference<>(obj);
        System.out.println(
            "-> Calling Display Function using strong object:");
        obj.Display();
        System.out.println("-> Object set to null");
        obj = null;
        obj = wobj.get();
        System.out.println(
            "-> Calling Display Function after retrieving from weak Object");
        obj.Display();
    }
}
class MyClass {
    void Display()
    {
        System.out.println("Display Function invoked ...");
    }
}


Java
// Java Program to show the usage of
// java.lang.ref.WeakReference Class
import java.lang.ref.WeakReference;
class GFG {
    public static void main(String[] args)
    {
        X obj = new X();
        WeakReference weakobj
            = new WeakReference(obj);
        System.out.println(
            "-> Retrieving object from weak reference using get ()");
        weakobj.get().show();
        System.out.println(
            "-> Is it possible to queue object using enqueue ()");
        System.out.println(weakobj.enqueue());
        System.out.println(
            "-> Checking if reference is queued using isEnqueued ()");
        System.out.println(weakobj.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 weak Object
Display Function invoked ...
输出
-> Calling Display Function using strong object:
Display Function invoked ...
-> Object set to null
-> Calling Display Function after retrieving from weak Object
Display Function invoked ...

WeakReference 类中的构造函数:

ParametersDescription
WeakReference ( T referent )This constructor creates a new weak reference that refers to the given object.
WeakReference ( T referent, ReferenceQueue q)This constructor creates a new weak reference that refers to the given object and is registered with the given queue.

从引用类继承的方法

MethodDescription
clear ( )This method clears this reference object.
enqueue ( )This method adds this reference object to the queue with which it is registered if any.
get ( )This method returns this reference object’s referent.
isEnqueued ( )This method tells whether this reference object has been enqueued, either by the program or by the garbage collector.

方法的实施:

Java

// Java Program to show the usage of
// java.lang.ref.WeakReference Class
import java.lang.ref.WeakReference;
class GFG {
    public static void main(String[] args)
    {
        X obj = new X();
        WeakReference weakobj
            = new WeakReference(obj);
        System.out.println(
            "-> Retrieving object from weak reference using get ()");
        weakobj.get().show();
        System.out.println(
            "-> Is it possible to queue object using enqueue ()");
        System.out.println(weakobj.enqueue());
        System.out.println(
            "-> Checking if reference is queued using isEnqueued ()");
        System.out.println(weakobj.isEnqueued());
    }
}
class X {
    void show()
    {
        System.out.println("show () from X invoked..");
    }
}
输出
-> Retrieving object from weak 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 weak reference using get ()
show () from X invoked..
-> Is it possible to queue object using enqueue ()
false
-> Checking if reference is queued using isEnqueued ()
false