concurrent hashmap accuracy

royjayd
royjayd used Ask the Experts™
on
If multiple threads are concurrently modifying a concurrenthashmap, how does it maintain the accuracy of data in it?

A simple answer would be highly appreciated.

thx.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Awarded 2011
Awarded 2011
Commented:

I guess, you need to takwe care of it yourslef if there is suuch danger:
THis is from
http://download.oracle.com/javase/1.4.2/docs/api/java/util/HashMap.html

If multiple threads access this 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.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method.
 
Mick BarryJava Developer
Top Expert 2010
Commented:
it uses locking internally to ensure operations are thread safe. Bit hard to explain it *simply* any better than that
Awarded 2011
Awarded 2011

Commented:

I guess it is different for HashTable, so if there is such issue you can use HashTable

Unlike the new collection implementations, Hashtable is synchronized.

http://download.oracle.com/javase/1.4.2/docs/api/java/util/Hashtable.html
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Mick BarryJava Developer
Top Expert 2010

Commented:
Its worth noting that there is no synchonization of any of its methods.
 
Top Expert 2016
Commented:
The table is divided into segments internally, so that only the smallest degree of locking is implemented. Write operations use locking to maintain thread safety. Read operations don't need to.
Top Expert 2016

Commented:
:)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial