My 'ConcurrentHashMap' is a static variable, so I can guarantee that there will be only one instance. but when I am going to sort it, I will convert it to a list and sort it, and then return it back to the new concurrentHashMap.
This is not an easy problem.
I can tell you that using ConcurrentHashMap will not make this thread safe. Also will not use the synchronizedMap wrapper. The problem is that sorting is not supported as a single atomic operation. Rather, it involves a series of API Maps operations, possibly with significant intervals between them.
I can come up with two approaches to solving this issue:
Avoid the need to sort first with the Card, which stores the keys in order; for example use ConcurrentSkipListMap.
Wrap the map class in a custom synchronized wrapper class using the synchronized sort method. The problem with this approach is that you are likely to re-add the concurrency bottleneck that you avoid using the ConcurrentHashMap.
And it is worth noting that it makes no sense to sort the HashMap or ConcurrentHashMap , because these cards will not preserve the order in which you sort the elements. You can use LinkedHashMap , which preserves the insertion order of the entry.
source share