-1

I’m setting up a new server, and want to put huge amount of elements to a hashmap at the server's initial time for save initial time, so I want the hashmap's put method is thread-safe. But after the initial time, the hashmap will no change and readonly, so I want the hashmap's get method is thread-unsafe is enough to get better performance.

If I use ConcurrentHashMap to set up the server, it will not good enough when get elements with multi-thread.

So is there any hashmap that put method with thread-safe and get method with thread-unsafe.

  • How can you _not_ have a thread-safe `get`? `get` does not mutate the map so it is guaranteed to be thread safe. – Sweeper Aug 28 '19 at 02:51
  • Since you create the read-only map at the server's initial time and no one is going to change it, I think `HashMap` is enough. – LHCHIN Aug 28 '19 at 03:00
  • Why does `put()` need to be thread-safe? – shmosel Aug 28 '19 at 03:10
  • 2
    **If I use ConcurrentHashMap to set up the server, it will not good enough when get elements with multi-thread.** have you proved that with sufficient benchmarks? – Ivan Aug 28 '19 at 03:23
  • @Sweeper just like this https://stackoverflow.com/questions/42380668/java-hashmap-readonly-thread-safety – sherlockwep Aug 28 '19 at 04:22
  • @LHCHIN yes,that just alright, but at the server's initial time, I want to speed up the process "put huge amount of elements to the HashMap with multi-thread,it must be thread-safe at server's initial time". if I use HashMap, it is too slow for initializing with single thread. I just want a high performance hashmap at initial time just like ConcurrentHashMap, and use it as a read-only simple map as HashMap. – sherlockwep Aug 28 '19 at 04:31
  • @shmosel speed up the process of server's initial time with multi-thread.(put huge amount of elements to the HashMap) – sherlockwep Aug 28 '19 at 04:35
  • What are the threads doing? Parallelizing `put()` itself won't speed things up. – shmosel Aug 28 '19 at 04:40
  • @Ivan another objective is to reduce the memory overhead. ConcurrentHashMap has a complicated internal structure. If I select a simple read-only hashmap(there are many 3rd implement of read-only hashmap with high performance, meanwhile with a smaller memory ). It will save much memory for the server. – sherlockwep Aug 28 '19 at 04:56
  • @shmosel If I use a read-only map such as HashMap. And parallelizing won't work. How can I speed up the initial time? Put huge amount of elements to the HashMap at the server's initial time(After the initial time, the Map is read-only). – sherlockwep Aug 28 '19 at 05:01
  • the server has 20 cores, so I can use such as 40 threads to help speeding up the inital time. – sherlockwep Aug 28 '19 at 05:09
  • I agreed with @Ivan about his comment. As far as I know, `ConcurrentHashMap` has bad performance only operating it with single thread. Therefore, have you compared the performance between `ConcurrentHashMap` and `HashMap` for `get` element? – LHCHIN Aug 28 '19 at 06:10
  • @LHCHIN I‘didn't compared the performance between ConcurrentHashMap and HashMap. At first, I choice ConcurrentHashMap,it work well, but I found it cost too much memory, so I droped this solution, and try to find a read-only simple map in order to reduce memory cost. But if select read-only simple map involve another problem "bad experience at servver initialhizing" – sherlockwep Aug 28 '19 at 07:29
  • @LHCHIN So is there any other solution to reduce the memory overhead compared with the ConcurrentHashMap's solution.I have more than 35 million elements, so I want to select a simple hashmap to reduce memory. My goal is "Reducing memory usage of very large HashMap" – sherlockwep Aug 28 '19 at 07:38
  • I have already done many ways to reduce memory, such as use bit-field with elements, but it is not enough, I want to reduce more memory. – sherlockwep Aug 28 '19 at 07:49

1 Answers1

0

If ConcurrentHashMap is not good for you due to additional footprint then you can do the following. For each thread you are using to populate map upon startup create its own HashMap and populate it. After that you merge all those maps into one HashMap (doing it in single thread or using 1 thread to merge maps from threads 1 and 2, using thread 3 to merge maps from threads 3 and 4 and so on and then repeating this until you end up with single large map). And after that you can use that HashMap in read-only mode.

Ivan
  • 8,508
  • 2
  • 19
  • 30