本文介绍了WeakReference 的 Java 文档中的矛盾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是关于理解 WeakReference 的 Java 文档

This question is about understanding the Java documentation for WeakReference

当我阅读 Java 的 WeakReference 时,我在文档中看到了这句话:

When I read about Java's WeakReference, I came across this sentence in the documentation:

假设垃圾收集器在某个时刻确定对象弱可达的时间.到时候会原子地清除对该对象的所有弱引用和所有弱引用对任何其他弱可达对象的引用对象可通过强引用和软引用链访问

但这对我来说没有意义.假设我有一个 WeakReference wrA 引用对象 A 和另一个 WeakReference wrB 引用对象 B. B 强烈引用 A(直接或间接通过链).现在根据文档,如果 A 变为弱可达,如果 B 那时已经弱可达,那么 wrB 将与 wrA 一起清除.但是,既然B在那个时候已经是弱可达的,那么在B的弱可达第一次被检测到的时候,wrB不应该早点被清除吗?

But this doesn't make sense to me. Suppose I have a WeakReference wrA referencing an object A and another WeakReference wrB referencing an object B. And B strongly refers to A (directly or indirectly through a chain). Now according to the document, if A becomes weakly reachable and if B is already weakly reachable at that time, then wrB will be cleared together with wrA. However, since B is already weakly reachable at that time, shouldn't wrB already be cleared at an early time when the weak reachability of B is first detected?

推荐答案

这同时发生,所以没有更早的时间.当 GC 启动时,所有当时不是强可达的对象(例如 A 和 B)都有资格在那时被清理.

This happens at the same time, so there is no earlier time. When a GC starts all objects (e.g. both A and B) which are not strongly reachable at that time are eligible to be cleaned up at that time.

这篇关于WeakReference 的 Java 文档中的矛盾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 07:06