我知道什么是WeakReference
,并且我阅读了它的文档以及许多博客和SO线程。然而,我仍然不清楚以下流程是否真的会发生。
- 对象
X
被强引用strRef
和弱引用weakRef
引用 strRef
正在随机线程上被清除weakRef
正在随机线程上取消引用- 将上一步中检索到的引用分配给
strRef
关于如何在 GC 上清除弱引用有很多讨论,但据我所知,GC 不会在每次引用清除后发生。因此,如果 GC 在步骤#2-#3 之间没有发生,那么弱可达对象可能会被上面的步骤#3“复活”。
这样的竞争条件非常不方便甚至危险,因此我认为应该有一些东西可以阻止它,但我想在这里完全确定。
那么,是否有一个规范可以确保弱引用对象无法复活,以及是否有与多线程相关的注意事项?
最佳答案
Therefore, it looks like a weakly reachable object could potentially be "resurrected" by the step #3 above if GC did not happen between steps #2-#3.
正确,即使GC已经运行,也不是所有的弱引用都必须被清除,例如当运行次要收集时,永久空间中的对象不会被清理。只有Full GC才能确保清除所有弱引用。
Such a race condition is very inconvenient and even dangerous, therefore I think that there should be something that prevents it,
鉴于您不知道后台线程何时清除强引用,因此您必须随时检查这一点。注意:清除强引用只是意味着将内存值设置为null
,仅此而已。
is there a specification that ensures that weakly referenced objects can't be resurrected, and are there any caveats related to multithreading?
即使是被丢弃的对象也可以通过在 finalise
方法中设置对 this
的引用来复活。我建议您不要依赖这种行为。引用设置不是消息来源,也不是有很多线程安全保证的操作。
I thought I could rely on weak references in order to subscribe these UI classes to an event bus, such that there will be no need to unsubscribe them later.
可以,但必须检查监听器是否仍然处于 Activity 状态。仅仅因为您可以获得对它的弱引用并不意味着您不打算丢弃它。
关于java - 能否通过WeakReference检索弱可达对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47902585/