加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何知道与WeakHashMap中已删除条目关联的值

发布时间:2020-12-15 02:20:54 所属栏目:Java 来源:网络整理
导读:我有这样的事情: private MapMyObj1,MyObj2 map = new WeakHashMapMyObj1,MyObj2(); ... somewhere in the code ... MyObj1 myObj1 = new MyObj1(); map.put(myObj1,new MyObj2(); ... myObj1 = null;... somewhere else in a thread ... (I would like to
我有这样的事情:

private Map<MyObj1,MyObj2> map = new WeakHashMap<MyObj1,MyObj2>();

 ... somewhere in the code ...
 MyObj1 myObj1 = new MyObj1();
 map.put(myObj1,new MyObj2();
 ...
 myObj1 = null;

... somewhere else in a thread ... (I would like to pass to a checkThis(MyObj2) method the Value associated with the entry that was removed from the Map)
/* something like this maybe */
while (true) {
    MyObj2 myObj2 = referenceQueue.remove().get();
    checkThis(myObj2);
}

GC进场时可能会删除MyObj1键,并且没有强烈的引用.

我想传递给checkThis(MyObj2)与被删除的键相关联的特定map-value对象(可能检查一个ReferenceQueue?)

我无法弄清楚如何将其放入代码中.

解决方法

从另一个答案的评论:

I want that once the session expires
(and there’s no strong reference to
that Session in my App Context) the
List is iterated and cancel is
called on all the remaining Futures
that are waiting to be executed. This
to avoid having scheduled jobs that
will run even if the User Session is
expired.

这听起来像HttpSessionBindingListener的工作

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读