HashTable write operation time, lock full table source code: Publicsynchronized v put (K key, V value) {//Make sure the value was not nullif (value = = null) {throw new NullPointerException (); } //makes sure the key is not already in the Hashtable.Entry tab[] = table;int hash = Key.hashcode ();int index = (hash & 0x7FFFFFFF)% Tab.length;For (entry<k,v> e = Tab[index]; E! = null; e = e.next) {if ((E.hash = = hash) && e.key.equals (key)) {V old = E.value;e.value = value;return old; } }Because this method is using the Synchronized method, it is possible to prevent multiple threads from using the method synchronized method object, Remove,putall,clear and other common methods are used synchronized method, therefore, When a thread calls one of the synchronized methods, the other thread cannot do any of the synchronized methods in the method object. Concurrenthashmap allows multiple modifications to be performed concurrently with CONCURRENTHASHMAP internal use segments (Segment) to represent these different parts, each of which is actually a small hash table. /*** The segments, each of which is a specialized hash table */final segment[] segments; A concurrenthashmap default has 16 segment segment, the source code is as follows:/*** The default number of concurrency control segments. **/static final int default_segments = +;Now look at the put method, source codePublic V put (K key, V value) {if (value = = null) throw new NullPointerException ();int hash = hash (key);return Segmentfor (hash). Put (key, hash, value, false); }If NULL is not supported in the visible method, if NULL is thrown, a null pointer exception operation is obtained through the Segmentfor method to obtain the Segment<k,v> in segment[]; Then save the key and value in segment and look at the put method of the Segment<k,v> objectv put (K key, int hash, V value, Boolean onlyifabsent) { Lock (); Lockingtry {int c = count;if (c + + > Threshold)//ensure capacityrehash ();hashentry[] tab = table;int index = hash & (tab.length-1);hashentry<k,v> first = (hashentry<k,v>) tab[index];hashentry<k,v> e = first;While (E! = null && (E.hash! = Hash | |!key.equals (E.KEY))) e = E.next; Find the old value V OldValue;if (E! = null) {//Exists modified valueoldValue = e.value;if (!onlyifabsent)e.value = value; }else {//does not exist create a new hashentry save dataoldValue = null;++modcount;Tab[index] = new hashentry<k,v> (key, hash, first, value);count = C;//Write-volatile }return oldValue;} finally { unlock ();//Release lock }Conclusion: Reference: http://www.iteye.com/topic/344876
"JAVA" Concurrenthashmap