Does java have a “LinkedConcurrentHashMap” data structure?

前端 未结 8 1702
猫巷女王i
猫巷女王i 2020-12-01 07:08

I need a data structure that is a LinkedHashMap and is thread safe.

How can I do that ?

8条回答
  •  抹茶落季
    2020-12-01 07:40

    The answer is pretty much no, there's nothing equivalent to a ConcurrentHashMap that is sorted (like the LinkedHashMap). As other people pointed out, you can wrap your collection using Collections.synchronizedMap(-yourmap-) however this will not give you the same level of fine grained locking. It will simply block the entire map on every operation.

    Your best bet is to either use synchronized around any access to the map (where it matters, of course. You may not care about dirty reads, for example) or to write a wrapper around the map that determines when it should or should not lock.

提交回复
热议问题