简体   繁体   中英

Should I use ConcurrentHashMap or Hashmap or SynchronizedMap?

I have created a hashmap outside my mutithreading code. There are going to be no changes in this hasmap later.

After this, I am starting two threads which will both be reading from this hashmap(yes, only read operations). If thread1 is reading from my hashmap object , can thread 2 also read at the same time? Or do I need a Concurrenthashmap or any other version of Map?

No you don't need concurrent Hash maps..but only since you don not do any modifications. You can read the same without any problem.

If thread1 is reading from my hashmap object, can thread 2 also read at the same time?

If you are sure that there are no write operations then you need not use Synchronization options at all, Go for a normal version of Map.

You can also use Immutable Map

A Map whose contents will never change, with many other important properties detailed at ImmutableCollection

如果只需要读操作,则无需使用同步。如果创建后未进行任何写操作,则使其不可变,因此没有主体可以更改它。不需要同步。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM