Is there java.util.concurrent equivalent for WeakHashMap?

前端 未结 6 1142
轻奢々
轻奢々 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:45

    If you are using Java 7 and above, this use case is solved in a thread-safe manner with ClassValue https://docs.oracle.com/javase/7/docs/api/java/lang/ClassValue.html If you require the use of remove, think carefully about concurrency and read the doc thoroughly.

    If you are using Java 6 or below. No, you have to synchronize a WeakHashMap.

提交回复
热议问题