Is there java.util.concurrent equivalent for WeakHashMap?

前端 未结 6 1140
轻奢々
轻奢々 2020-12-08 13:38

Can the following piece of code be rewritten w/o using Collections.synchronizedMap() yet maintaining correctness at concurrency?

Collections.sy         


        
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 13:53

    Cafeine is a popular competitor of Guava cache.

    - keys automatically wrapped in weak references
    - values automatically wrapped in weak or soft references
    

    usage:

    LoadingCache graphs = Caffeine.newBuilder()
     .weakKeys()
     .weakValues()
     .build(key -> createExpensiveGraph(key));
    

提交回复
热议问题