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?
Asked
Active
Viewed 331 times
2
-
4On what hardware do you intend to generate 100,000 concurrent requests to a CHM? In any case, there's no limit to the number of concurrent requests it can handle. – Marko Topolnik Oct 05 '16 at 11:58
-
What matters is not the number of concurrent request in absolute terms, but per unit of time: is it 100k per second? per minute? per day? – assylias Oct 05 '16 at 11:58
-
@ Marko Topolnik it will be used in web server. So hardware will be dedicated web server. – Rajesh N Oct 05 '16 at 12:01
-
@assylias it can be random. the request will be varies depending on web services call from users. – Rajesh N Oct 05 '16 at 12:03
-
2That was a rhetorical question. The point is that there _won't_ be 100,000 concurrent CHM method invocations. – Marko Topolnik Oct 05 '16 at 12:03
-
I am storing locale objects in static ConcurrentHashmap to reuse it – Rajesh N Oct 05 '16 at 12:06
2 Answers
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:
- Create your instance of
ConcurrentHashMap
with the constructorConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel)
in order to set theconcurrencyLevel
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. - 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
- 43,537
- 11
- 94
- 122
-
check this http://stackoverflow.com/questions/113511/best-implementation-for-hashcode-method – Nicolas Filotto Oct 05 '16 at 12:44
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
- 831
- 6
- 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