"JAVA" Concurrenthashmap

Source: Internet
Author: User


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&gt 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.