2

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 ConcurrentHashMap to store Locale objects? Can ConcurrentHashMap handle 100k concurrent requests?

James Z
  • 12,104
  • 10
  • 27
  • 43
Rajesh Nasit
  • 5,035
  • 2
  • 37
  • 50

2 Answers2

2

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.

Nicolas Filotto
  • 41,524
  • 11
  • 90
  • 115
0

ConcurrentHashMap is a very effictient data structure. You can start with it, and measure/test performance to ensure it is sufficient for you.

olsli
  • 781
  • 5
  • 8
  • I've not used jMeter. I would start with generating fake requests and profile app using jmc or jvisualvm – olsli Oct 05 '16 at 12:14