任何人都知道 WeakReferenceCollection 来保存侦听器
我想避免由于未删除的侦听器而导致类加载器泄漏。为了促进这一点,我相信对听众的弱引用或软引用会有所帮助。严格来说,我需要的唯一 Collection 方法是 add() 和 iterator()。
是否有任何 foss 库具有使用弱引用或软引用的集合或列表。
任何建议将不胜感激,否则我会自己写。
I would like to avoid class loader leaks due to un-removed listeners. To facilitate this i believe weak or soft references to listeners would help. Strictly the only Collection methods i need are add() and iterator().
Are there any foss libraries with a Set or List that uses Weak or Soft References.
Any suggestions would be appreciated, otherwise i will write my own.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您实际上可以使用
WeakHashSet
来达到此目的。只需对所有值使用null
即可。编辑
WeakHashMap 用于 WeakHashSet,Boolean.TRUE 用于值。
You could actually use
WeakHashSet
for this purpose. Just usenull
for all of the values.EDIT
WeakHashMap for WeakHashSet and Boolean.TRUE for values.
有一个java.util.WeakHashMap。我不确定 JDK 中的 List 和 Set 是否并行,尽管使用 WeakReferences 编写 WeakList 应该相当简单。有一个 WeakReferenceList 的示例实现。
请注意,将侦听器存储为 WeakReference 可能很危险,因为许多人将侦听器编码为匿名类,这很容易超出范围并被垃圾回收。
There is a java.util.WeakHashMap. I am not sure whether parallel for List and Set in JDK, though it should be fairly simple to write a WeakList using WeakReferences. There is an example implementation of WeakReferenceList.
Please note that storing listeners as WeakReferences can be dangerous, as many people code listeners as anonymous classes which can easily get out of reach and gc-ed.
我自己使用 WeakHashMap 作为存储桶编写了微不足道的内容。
Was trivial wrote my own using WeakHashMap as the bucket.