有人可以向我解释一下什么时候使用 MapMaker 或 WeakHashMaps 有用吗?
我读到很多人都非常喜欢 MapMaker Google Guava(集合),但是我看不到它有任何好的用途。
我已阅读 javadoc,它说它的行为类似于 ConcurrentHashMap。它还说 new MapMaker().weakKeys().makeMap()
几乎总是可以用作 WeakHashMap。
但是,阅读 ConcurrentHashMap 和 WeakHashMap 让我想知道什么时候使用它有用?在我看来,你不能保证你在地图中放入的任何内容都会在那里,还是我误解了?
I have read many people really like the MapMaker of Google Guava (Collections), however I cannot see any good uses of it.
I have read the javadoc, and it says that it behaves like ConcurrentHashMap. It also says new MapMaker().weakKeys().makeMap()
can almost always be used as a drop-in replacement for WeakHashMap.
However, reading the javadocs of ConcurrentHashMap and WeakHashMap makes me wonder when it is useful to use it? It seems to me that you cannot have a guarantee that whatever you put in the map will be there, or have I misunderstood?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ConcurrentHashMap
是一个可以在多线程环境中安全使用的Map
。它比常规Map
的同步版本更好,因为并发意味着不同的线程通常可以无阻塞地访问此映射。ConcurrentHashMap
is aMap
which may be safely used in multi-threading environment. It is better than synchronized version of regularMap
because concurrency means that different threads are often available to access this map without blocking.当有人(除了地图之外)引用该条目时,
WeakHashmap
条目将保留在地图中。如果除了映射之外没有其他人保留对该条目的引用,则可以在下一次 GC 运行时删除该条目。A
WeakHashmap
entry will be kept in the map while someone (other than the map) is referencing the entry. If nobody else thant the map is keeping a reference on the entry, then the entry can be removed on a next GC run....这就是它的重点。如果您不想(或无力)将对象无限期地保留在内存中,那么弱引用很有用。考虑以下用例:您需要将信息与类相关联。现在,由于您正在一个可能会重新加载类的环境中运行(例如 Tomcat 或 OSGi 环境),因此您希望垃圾收集器能够在认为安全时尽快回收旧版本的类。
实现此目的的初步尝试可能看起来像
这里的问题是:这会将所有类永远保留在
cache
成员中(或者至少,除非它们被手动删除),迫使垃圾收集器保留无限期地使用它们,包括从类引用的所有内容(静态成员值、类加载器信息等)通过使用弱引用,垃圾收集器可以在不存在对该类的其他引用(通常是实例)时立即回收该类的旧版本。另一方面:只要存在此类引用,就保证该值也可以从弱引用对象访问,因此是缓存表中的有效键。
将并发性和其他暴行添加到图片中,您就可以选择 MapMaker 还提供的内容...
...and that's somewhat the point of it. Weak references are useful, if you don't want to (or cannot afford to) retain an object indefinetly in memory. Consider the following use case: you need to associate information with classes. Now, since you are running in an environment, where classes might get reloaded (say, a Tomcat, or OSGi environment), you want the garbage collector to be able to reclaim old versions of a class as soon as it deems safe to do so.
An initial attempt to implement this, might look like
The problem here is: this would keep all classes in the
cache
member forever (or at least, unless they are manually removed), forcing the garbage collector to retain them indefinitly, including everything referenced from the class (static member values, class loader information, ...)By using weak references, the garbage collector can reclaim old version of the class as soon as no other references to it (usually instances) exist. On the other hand: as long as such references exist, the value is guaranteed to be also reachable from the weak reference object, and thus, is a valid key in the cache table.
Add concurrency and other atrocities to the picture, and you are at what
MapMaker
optionally also provides...MapMaker
的特点是,您构建的地图类型有很多选项,这使得这些地图能够满足多种用途。Function
自动检索与给定键关联的值(如果映射中尚不存在该值)。这与软值和/或过期时间很好地结合在一起。在映射驱逐条目后(由于内存需求或过期),下次请求与该键关联的值时,它将自动检索并再次缓存在映射中。The thing about
MapMaker
is that there are many options for the kind of map you build, which enables those maps to serve many purposes.Function<K, V>
to automatically retrieve the value associated with a given key if it isn't already in the map. This combines well with soft values and/or expiration times. After an entry is evicted by the map (due to memory demand or expiration), the next time the value associated with that key is requested it will automatically be retrieved and cached in the map once more.