Rajesh Rajesh - 1 year ago 115
Java Question

Java + can I use ConcurrentHashMap to handle 100k concurrent request?

I am developing the custom i18N library. in that, I have developed one function for getting locale object as per as input country and language.
I don't want to initialize locale object for every request. Can I use

to store Locale objects? Can
handle 100k concurrent requests?

Answer Source

Assuming that you use a version of Java older than 8, if you want to maximize the concurrency level of a ConcurrentHashMap, you need to:

  1. Create your instance of ConcurrentHashMap with the constructor ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) in order to set the concurrencyLevel corresponding to the total amount of segments to use knowing that by default it is 16 and choose it wisely as using a significantly higher value than what you need can waste space and time, and a significantly lower value can lead to thread contention.
  2. Implement the method hashCode() of your key properly in order to distribute your entries the best you can over the different segments.

In Java 8, the implementation of an ConcurrentHashMap has completely been reviewed such that you don't have segments anymore, the concurrencyLevel is now used as a sizing hint nothing more so #1 is no more really needed but #2 is still a good practice.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download