Digression : Why hashcode Conduct spread? Make full use of key.hashCode() The height of 16 Bit information , Guarantee hash The distribution is more dispersed ,

The expansion operation is new 2 A new table twice the size of the original table , And copy the original table node into a new table , There is no or little modification to the original table . When all nodes in the original table have been copied to the new table , The original table will be garbage collected .

stay jdk7 Medium HashMap In the implementation class , Array + Linked list . The expansion operation is to expand the nodes of the original array one by one hash Calculation , And then hook them one by one to the new array , So it's not a mechanism based on replication nodes .
stay jdk7 Medium ConcurrentHashMap In the implementation class , paragraph (segment)+ Array + Linked list . The expansion operation is to traverse the array elements first , Traverse the linked list over each array element , Find the end of the list n Nodes ( this n Nodes in the new array must belong to the same array position ), Put this n The first node is attached to the array position of the new array , It's also called lastRun Mechanism . As for the head node of the original array to the reciprocal n Nodes between nodes , Go over it again , The mechanism of copying each node is used to attach to the new array .

jdk8 Medium HashMap In the implementation class , Array + Linked list / Red and black trees . The expansion operation is to expand each node of the original array hash Value and the length of the original array “ And ” operation , The result is equal to the 0 Represents that the node position remains unchanged , In the same position as the new array , Otherwise, the position of the node in the new array is [j + oldCap] On .
as a result of : Expansion is to double the length of the array , If the original length is 16( Binary is 10000, Mask 1111), The new length is 32( Binary is 100000, Mask 11111), So when calculating the position of the node ,hash The value is low 4 Bit participation calculation , After expansion, it becomes hash Low value 5 Bit participation calculation , In this case , When the highest bit of the operation is the fifth bit , yes 1 when , It must be in a new position after expansion , yes 0 when , The necessary position remains unchanged , Because the length of the original array 16 The fifth bit of the binary is 1, So by putting the node's hash Value and the length of the original array , You can know whether the node in the new array remains relatively unchanged or falls in a higher position .

jdk8 Medium ConcurrentHashMap In the implementation class , Array + Linked list / Red and black trees . The expansion operation is completed by multiple threads , Compared with jdk7 Version expansion ,jdk8 The expansion of is a gradual expansion , It's not done overnight . The length of the original array is n As the total number of expansion tasks , Cut into m Block as m Small tasks , Each small task has and only has one thread to complete the expansion ( Because the length of the expanded array is the original 2 times , The node is either in the relative position of the new array i, Either in i+OldTableSize It's about , So when other threads expand other small tasks , There is no position conflict with the current thread ). For capacity expansion , The linked list also looks for lastRun And then hook it to the new array , The previous node is then linked to the new array through the copy mechanism .

HashMap Concurrency issues : The cause of the dead cycle
Hashmap Of Resize Including expansion and ReHash Two steps ,ReHash In the case of concurrency, a link may be formed . because , The linked list adopts the head insertion method , When transferring the original array to a new array , Will traverse the list node from front to back , The head insertion mechanism just reverses the relative order of the nodes in the new array from that in the original array . In concurrency , If the original node order is changed by thread A It's upside down , And the suspended thread b After resuming execution , Take the node and sequence before expansion and continue to complete the first cycle , And then follow A The order of the linked list after thread expansion rearranges the order of the linked list , That is to say, the order is reversed , And eventually it's a ring .

jdk7 and 8 About China HashMap and concurrentHashMap Summary of the expansion process of , as well as HashMap More articles on dead cycle

  1. I'll start again :java Medium HashMap、ConcurrentHashMap and Hashtable The understanding of

    I'll start again :java Medium HashMap.ConcurrentHashMap and Hashtable The understanding of One . Preface A lot of knowledge will always be forgotten after learning or using , But if we understand that these are just the principles behind it , And remember ...

  2. debugging JDK Source code - Step by step HashMap Yes? Hash And capacity

    debugging JDK Source code - Step by step HashMap Yes? Hash And capacity debugging JDK Source code -ConcurrentHashMap Realization principle debugging JDK Source code -HashSet Realization principle debugging JDK Source code - debugging JDK Source code ...

  3. HashMap When will the expansion take place

    HashMap Capacity expansion : When HashMap More and more elements in , The probability of collision is higher and higher ( Because the length of the array is fixed ), So in order to improve the efficiency of query , on HashMap Expand the size of the array , The operation of array expansion will also appear in A ...

  4. Java7/8 Medium HashMap and ConcurrentHashMap Full resolution

    Java7/8 Medium HashMap and ConcurrentHashMap Full resolution I'll send you an article today ” hydrological ”, Maybe many readers will say they don't understand , But I want to introduce it as an indispensable part of the concurrent sequence article . I thought I couldn't spend it ...

  5. Java About China Map Use (HashMap、ConcurrentHashMap)

    In daily development Map May be Java The most commonly used class in collection framework , When we use it routinely HashMap You may often see the following code : Map<Integer, String> hashMap = new ...

  6. Java7/8 in HashMap and ConcurrentHashMap The comparison and analysis of traditional Chinese Medicine

    You may use HashMap More , be relative to ConcurrentHashMap I'm not very familiar with .ConcurrentHashMap yes JDK 1.5 New collection added , To ensure thread safety , promote Map ...

  7. JDK7 And JDK8 in HashMap The implementation of the

    JDK7 Medium HashMap HashMap The bottom layer maintains an array , Every item in the array is a Entry transient Entry<K,V>[] table; We ask HashMap What's placed in ...

  8. java7,java8 in HashMap and ConcurrentHashMap brief introduction

    One :Java7 Medium HashMap structure : HashMap It's an array , Then each element in the array is a one-way linked list . Each element in a linked list is called a Entry example ,Entry Contains four properties :key, value, ...

  9. Java7 And Java8 Medium HashMap and ConcurrentHashMap Summary of knowledge points

    JAVA7 Java7 Of ConcurrentHashMap There are many locks in the car , Each lock is used for part of the data , So when multiple threads access the data of different data segments in the container , There will be no lock contention between threads , So it can effectively improve the efficiency of concurrent access . this ...

Random recommendation

  1. iOS Development series -- sound recording

    stay AVFoundation There's another one in the frame AVAudioRecorder Class deals with recording operations , It also supports multiple audio formats . And AVAudioPlayer similar , You can think of it as a recorder control class , Here are the common genera ...

  2. Tencent cloud centos 7 Deploy dotnetcore+Angular2 practice

    Copyright notice : This article is written by Qu Zhengbin , Reprint please indicate the source :  Link to the original article :https://www.qcloud.com/community/article/239 source : Tengyun Pavilion  https://www.qclo ...

  3. Think Python - Chapter 10 - Lists

    10.1 A list is a sequenceLike a string, a list is a sequence of values. In a string, the values are ...

  4. MySQL Master slave architecture configuration details

    Whatever kind of database , Data security is crucial , So master the safe backup function of database , As a developer , Especially a necessary skill for back-end developers .MySQL Database built-in replication function , It can help us to backup data in other places , Reading and writing ...

  5. MyReport Report engine 2.1.0.0 new function

    Four simple examples of operations are supported 1: Total score , Simply add Examples 2: Average . Unless mixed with addition ( It can recognize multiplication and division before addition and subtraction , Bracket precedence is not supported ) New line number function   Generate line numbers directly Add a variety of formatting settings to fix several found Bug. Optimization generation ...

  6. A simple one based on BIO Of RPC frame

    github Address :https://github.com/Luyu05/BioRpcExample PART1: Let's look at the composition of the project as a whole among bio-rpc-core It's called rpc frame bio-rpc- ...

  7. Use shell install lnmp

    1. brief introduction Use shell Script installation lnmp, Just being lazy , Usually install some things are written into a script , It is convenient to install other machines in the future without looking for documents . PHP edition 5.6.6 MYSQL edition 5.6.26 NGINX edition 1. ...

  8. Finally no longer in the muddle force mysql Native statement ,orm Super debut

    import sqlalchemy from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import cre ...

  9. openssl upgrade

    Red hat 6.2 upgrade openssl Method yum install nginx when , Find out openssl Dependency package error , Tips openssl The required version is 1.0.1 , But the current version is 1.0.0. Through online introduction , take openssl Source code rewrite ...

  10. ExtJS Get the data of the selected row in

    listeners: { select:function(rowModel,record){ var data = rowModel.getLastSelected(); console.log(&q ...