-3

Hashmap is not synchronized. Suppose I want to use Hashmap in multithreaded environemnt. what will be the behaviour of hashmap?

  • Its behavior will be undefined... – Sotirios Delimanolis Sep 04 '14 at 06:40
  • 4
    My car doesn't have a brakes. What will happen if I drive it at 100 KMPH?. – TheLostMind Sep 04 '14 at 06:42
  • The behavour will be of a HashMap. The Map does'nt know how it is accessed (synchronized or not). It depends all on the **use case**. – PeterMmm Sep 04 '14 at 06:43
  • There is a blog article out there explaining in great detail what happened to someone who used it. The result was deadlock with the expected hanging threads. Can't find the link anymore though. In short: use Collections.synchronizedMap(new HashMap<>()); – nablex Sep 04 '14 at 06:49
  • 1
    That depends on how you use the `HashMap`, the implementation of `HashMap` you are using, the types of objects it contains, the type of computer and operating system, and random factors. It is possible to write thread-safe code that uses data structures that are not themselves thread-safe. Your question lacks the detail to be answerable. – Raedwald Sep 04 '14 at 07:08
  • Possible duolicate of http://stackoverflow.com/questions/11050539/using-hashmap-in-multithreaded-environment – Raedwald Sep 04 '14 at 07:09

2 Answers2

4

Since it is not synchronized, it's not thread safe.

Use ConcurrentHashMap, if you are looking for a synchronization .

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 1
    +1 I am agree with you. – Subhrajyoti Majumder Sep 04 '14 at 06:45
  • -1: use Collections.synchronizedMap() (not actually a -1 but I don't entirely agree) – nablex Sep 04 '14 at 06:51
  • 1
    I found that always misleading: of course methods are sync and there are some special ones (putIfAbsent). But if you are doing multiple operations (in a block) in diferent threads {get(); remove(); put();} you propably still need to sync externally. It is not always substitute HashMap by ConcurrentHashMap and you are done. – PeterMmm Sep 04 '14 at 06:52
  • @nablex Why I have to write extra line of code?? Designers already wrote code for me :). Best part is it works without any bugs. – Suresh Atta Sep 04 '14 at 06:54
  • @PeterMmm I follow your point but it depends on the usage (see e.g. http://stackoverflow.com/questions/510632/whats-the-difference-between-concurrenthashmap-and-collections-synchronizedmap). – nablex Sep 04 '14 at 07:45
  • @sᴜʀᴇsʜᴀᴛᴛᴀ Using the static method prevents you from having a dependency on the specific implementation. – nablex Sep 04 '14 at 07:47
1

Concurrent modification will cause unpredicted in HashMap out put and you will experience with ConcurrentModificationException .

Note that this implementation is not synchronized. If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.)

Documentation

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103