Java – Concurrent HashMap: check size

Concurrent HashMap can solve the synchronization problem seen in HashMap Therefore, if we work with a synchronization key with HashMap, additions and deletions will be fast If the mulitple thread checks the size of the concurrent HashMap, what about checking the HashMap size? We also need to synchronize keywords as follows:

public static synchronized getSize(){
     return aConcurrentHashmap.size();
}

Solution

concurentHashMap. Size () will return the size known at the time of the call, but when you use this number, it may be an old value because another thread added / deleted items during this period

However, the whole purpose of concurrent maps is that you don't need to synchronize it because it is a thread safe collection

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>