除了固定超时之外,还有其他方法可以知道WeakHashMap在密钥变得弱可访问后何时更新其条目?
例如,这段代码使对键的强引用无效,除非添加超时,否则仍会打印条目在映射中。
WeakHashMap<Integer, Integer> map = new WeakHashMap<>();
Integer i = 1000;
map.put(i, 1);
System.out.println(map.size()); // prints 1
i = null;
System.gc();
//Thread.sleep(0);
System.out.println(map.size()); // without sleep prints 1, with sleep prints 0
有没有更优雅的方法可以知道 WeakReferences 何时完成更新?