a Learner
a Learner

Reputation: 5042

ConcurrentHashMap operations are thread safe

Java Docs for the ConcurrentHashMap says,

even though all operations are thread-safe

What is the meaning when we say all operations of ConcurrentHashMap are thread safe?

EDIT: what i mean to ask is that suppose there is put() operation. then according to above statement put() in CHM is thread safe. What does this mean?

Upvotes: 0

Views: 1721

Answers (5)

GaryF
GaryF

Reputation: 24340

From Wikipedia:

A piece of code is thread-safe if it only manipulates shared data structures in a manner that guarantees safe execution by multiple threads at the same time.

To answer your expanded question, if multiple threads were to execute put() the effect would be that the last one to run would set the value for that key in the map. All of the puts would happen in some sequence, but they would not interfere with each other. How might they interfere without a concurrency guarantee? Well, put() returns null if no value had previously been associated with the mapping or the previous value. If two puts happened on a non-concurrent map they can both get the same return value from the put.

This sequence is possible without concurrency:

Thread1: map.put("key1", "value1") => null

then

Thread2: map.put("key2", "value2") => "value1"
Thread3: map.put("key3", "value3") => "value1"

If Thread3 got in just after Thread2, it might see "value1" rather than "value2", even though that's not what it replaces. This won't happen in a concurrent map.

Upvotes: 1

Thread-safety means that an object can be used simultaneously by multiple threads while still operating correctly. In the specific case of ConcurrentHashMap, these characteristics are guaranteed:

  • Iterators produced by the map never throw ConcurrentModificationException, and they'll iterate in an order that's fixed when they're created. They may or may not reflect any modifications made while the map is being accessed. Ordinary HashMap iterators will throw exceptions if modified while a thread is iterating over them.
  • Insertion and removal operations are thread-safe. Ordinary HashMaps might get into an inconsistent internal state if multiple threads tried to insert or remove items simultaneously, especially if modifications required a rehash.

Upvotes: 1

Manu
Manu

Reputation: 453

It means that all the operations you do to add/delete objects into your hash map is thread safe, but retrieving is not thread safe. Means that when you added a object in a perfect thread safe environment, after that moment that object should be visible to all the thread who are retrieving object from this MAP. But this thing is not guaranteed here.

Upvotes: 0

TKV
TKV

Reputation: 2663

What thread safety means is that you are permitted to share a ConcurrentHashMap object across multiple threads, and to access/modify that object concurrently without external locking.

Upvotes: 1

Pasquale Anatriello
Pasquale Anatriello

Reputation: 2355

that if two threads will concurrently try to do operations on the ConcurrentHashMap you are guaranteed that the operations will not leave the data structure in an inconsistent state. That's not something other non concurrent data structure guarantee.

Upvotes: 0

Related Questions