当前位置: 首页 > news >正文

企业网站和信息化建设金蝶seo快速优化技术

企业网站和信息化建设金蝶,seo快速优化技术,在线销售网站设计文献,音乐网站开发技术一.麻烦讲述一下Hashmap的扩容原理 jdk1.8中的hashmap扩容原理 1.put流程图 首先贴一张图(图片来源于传送门),多谢大佬的美图,此图已经完美的描述了put的整个流程,我也就不想自己画了,嘿嘿: 2.hashmap中几个比较重…

一.麻烦讲述一下Hashmap的扩容原理

jdk1.8中的hashmap扩容原理

1.put流程图

首先贴一张图(图片来源于传送门),多谢大佬的美图,此图已经完美的描述了put的整个流程,我也就不想自己画了,嘿嘿:
put流程图

2.hashmap中几个比较重要的成员变量

HashMap在底层数据结构上采用了数组+链表+红黑树,在其源码中有几个比较重要的成员变量需要记住,这几个成员变量也是在扩容中肯定会遇到的:

// 默认的初始化容量16,必须为2的幂
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
//默认的负载因子0.75f
static final float DEFAULT_LOAD_FACTOR = 0.75f;
//当某个桶的阈值大于8,会将链表的结构转换为红黑树
static final int TREEIFY_THRESHOLD = 8;
//当某个桶的阈值小于6时,会将原本的树结构转换为链表结构
static final int UNTREEIFY_THRESHOLD = 6;
//当Node数组的长度大于64才允许将链表转换为红黑树,否则应该直接扩容而不是将链表进行树化
//此处的含义就是如果一个链表的长度超过了8,但是Node数组的长度小于64的话,一般以扩容来处理,
而不是将链表树化
static final int MIN_TREEIFY_CAPACITY = 64;
//阈值=capacity * load factor
int threshold;

3.扩容的详细步骤

public V put(K key, V value) {return putVal(hash(key), key, value, false, true);
}final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {Node<K,V>[] tab; Node<K,V> p; int n, i;if ((tab = table) == null || (n = tab.length) == 0)n = (tab = resize()).length;if ((p = tab[i = (n - 1) & hash]) == null)tab[i] = newNode(hash, key, value, null);else {Node<K,V> e; K k;if (p.hash == hash &&((k = p.key) == key || (key != null && key.equals(k))))e = p;else if (p instanceof TreeNode)e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);else {for (int binCount = 0; ; ++binCount) {if ((e = p.next) == null) {p.next = newNode(hash, key, value, null);if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1sttreeifyBin(tab, hash);break;}if (e.hash == hash &&((k = e.key) == key || (key != null && key.equals(k))))break;p = e;}}if (e != null) { // existing mapping for keyV oldValue = e.value;if (!onlyIfAbsent || oldValue == null)e.value = value;afterNodeAccess(e);return oldValue;}}++modCount;if (++size > threshold)resize();afterNodeInsertion(evict);return null;}

首先从put和putVal中我们发现扩容的核心源码是resize():
在分析resize()方法之前,我们首先了解一下在那些情况下hashmap会产生扩容动作:
在hashmap中有三种情况会触发扩容,分别是:
第一种:使用默认构造方法初始化HashMap。从前文可以知道HashMap在一开始初始化的时候会返回一个空的table,并且thershold为0。因此第一次扩容的容量为默认值DEFAULT_INITIAL_CAPACITY也就是16。同时threshold = DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR = 12。
第二种:指定初始容量的构造方法初始化HashMap。那么从下面源码可以看到初始容量会等于threshold,接着threshold = 当前的容量(threshold) * DEFAULT_LOAD_FACTOR。

第三种:HashMap不是第一次扩容。如果HashMap已经扩容过的话,那么每次table的容量以及threshold量为原有的两倍。

这边也可以引申到一个问题HashMap是先插入还是先扩容:HashMap是先插入数据再进行扩容的,但是如果是刚刚初始化容器的时候是先扩容再插入数据(这一点可以从putValue方法源码中看出)。
接下来是重点resize()方法:

final Node<K,V>[] resize() {Node<K,V>[] oldTab = table;int oldCap = (oldTab == null) ? 0 : oldTab.length;int oldThr = threshold;int newCap, newThr = 0;if (oldCap > 0) {if (oldCap >= MAXIMUM_CAPACITY) {threshold = Integer.MAX_VALUE;return oldTab;}else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&oldCap >= DEFAULT_INITIAL_CAPACITY)newThr = oldThr << 1; // double threshold}else if (oldThr > 0) // initial capacity was placed in thresholdnewCap = oldThr;else {               // zero initial threshold signifies using defaultsnewCap = DEFAULT_INITIAL_CAPACITY;newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);}if (newThr == 0) {float ft = (float)newCap * loadFactor;newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?(int)ft : Integer.MAX_VALUE);}threshold = newThr;@SuppressWarnings({"rawtypes","unchecked"})Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];table = newTab;if (oldTab != null) {for (int j = 0; j < oldCap; ++j) {Node<K,V> e;if ((e = oldTab[j]) != null) {oldTab[j] = null;if (e.next == null)newTab[e.hash & (newCap - 1)] = e;else if (e instanceof TreeNode)((TreeNode<K,V>)e).split(this, newTab, j, oldCap);else { // preserve orderNode<K,V> loHead = null, loTail = null;Node<K,V> hiHead = null, hiTail = null;Node<K,V> next;do {next = e.next;if ((e.hash & oldCap) == 0) {if (loTail == null)loHead = e;elseloTail.next = e;loTail = e;}else {if (hiTail == null)hiHead = e;elsehiTail.next = e;hiTail = e;}} while ((e = next) != null);if (loTail != null) {loTail.next = null;newTab[j] = loHead;}if (hiTail != null) {hiTail.next = null;newTab[j + oldCap] = hiHead;}}}}}return newTab;}

从resize()方法中可以看到一点:最重要的一个判断是(e.hash & oldCap),从此处会将链表分成两段低位链表和高位链表,低位链表的节点位置不动,高位链表节点的位置是原来的位置加上老的链表的位置。
原理如下:
原table中的数据索引到新的table中,要么保持位置不变,要么位置= oldCap(原表大小) + 原表索引
先放一下求余公式:X % 2^n = X & (2^n - 1) (x对2的n次方求余)
length = 2的n次方
原表索引 = (hash的后N位) & (length - 1)
新表索引 = (hash的后N+1位) & (length << 1 - 1)
如果还有不理解的可以看这位大神的博客:传送门

jdk1.7中为什么多线程情况下会出现死循环?

答案:直接一个传送门


文章转载自:
http://rudimentary.Ljqd.cn
http://estuarine.Ljqd.cn
http://comestible.Ljqd.cn
http://pelasgi.Ljqd.cn
http://councilwoman.Ljqd.cn
http://fordize.Ljqd.cn
http://dblclick.Ljqd.cn
http://wrongheaded.Ljqd.cn
http://reprimand.Ljqd.cn
http://puzzlepated.Ljqd.cn
http://millstream.Ljqd.cn
http://dasd.Ljqd.cn
http://leptocephalic.Ljqd.cn
http://adulation.Ljqd.cn
http://burrawang.Ljqd.cn
http://uncoded.Ljqd.cn
http://lateroversion.Ljqd.cn
http://planetarium.Ljqd.cn
http://tola.Ljqd.cn
http://whetter.Ljqd.cn
http://parleyvoo.Ljqd.cn
http://affinitive.Ljqd.cn
http://barents.Ljqd.cn
http://packinghouse.Ljqd.cn
http://mythoheroic.Ljqd.cn
http://nonobjectivity.Ljqd.cn
http://arch.Ljqd.cn
http://conchiferous.Ljqd.cn
http://serendipitous.Ljqd.cn
http://aboral.Ljqd.cn
http://clogger.Ljqd.cn
http://pantagruelian.Ljqd.cn
http://cabby.Ljqd.cn
http://unbloody.Ljqd.cn
http://langlaufer.Ljqd.cn
http://unanswered.Ljqd.cn
http://coon.Ljqd.cn
http://rechange.Ljqd.cn
http://groundsill.Ljqd.cn
http://grill.Ljqd.cn
http://karachi.Ljqd.cn
http://lebensraum.Ljqd.cn
http://poised.Ljqd.cn
http://physiotherapeutic.Ljqd.cn
http://phorbol.Ljqd.cn
http://siangtan.Ljqd.cn
http://zealous.Ljqd.cn
http://galactokinase.Ljqd.cn
http://hypermetrical.Ljqd.cn
http://angiocarpous.Ljqd.cn
http://carla.Ljqd.cn
http://theonomous.Ljqd.cn
http://emcee.Ljqd.cn
http://rumour.Ljqd.cn
http://frigid.Ljqd.cn
http://workable.Ljqd.cn
http://inundate.Ljqd.cn
http://mustachio.Ljqd.cn
http://aseptic.Ljqd.cn
http://destroy.Ljqd.cn
http://vegete.Ljqd.cn
http://qcd.Ljqd.cn
http://musical.Ljqd.cn
http://revalidate.Ljqd.cn
http://squiggly.Ljqd.cn
http://avery.Ljqd.cn
http://akureyri.Ljqd.cn
http://extensible.Ljqd.cn
http://reindeer.Ljqd.cn
http://overbold.Ljqd.cn
http://crossruff.Ljqd.cn
http://balefire.Ljqd.cn
http://fatsoluble.Ljqd.cn
http://nicotin.Ljqd.cn
http://flukey.Ljqd.cn
http://alabamian.Ljqd.cn
http://swaybacked.Ljqd.cn
http://larine.Ljqd.cn
http://mayor.Ljqd.cn
http://olfactory.Ljqd.cn
http://boracite.Ljqd.cn
http://aiche.Ljqd.cn
http://omission.Ljqd.cn
http://pyrgeometer.Ljqd.cn
http://dinero.Ljqd.cn
http://shemozzle.Ljqd.cn
http://blastocele.Ljqd.cn
http://unroyal.Ljqd.cn
http://ial.Ljqd.cn
http://tamari.Ljqd.cn
http://economy.Ljqd.cn
http://unifier.Ljqd.cn
http://outstation.Ljqd.cn
http://hoof.Ljqd.cn
http://hyetometer.Ljqd.cn
http://polarizability.Ljqd.cn
http://predistortion.Ljqd.cn
http://quartersaw.Ljqd.cn
http://scurrility.Ljqd.cn
http://gauger.Ljqd.cn
http://www.15wanjia.com/news/93712.html

相关文章:

  • 宠物出售的网站怎么做原创文章代写
  • 黄冈网站推广收费标准360收录查询
  • 成都网站建设优化推广告公司取名字参考大全
  • 官方微信公众号班级优化大师下载安装
  • 现在大家做电商网站用什么源码aso优化费用
  • 记事本怎么做网站今日国际新闻头条15条
  • 企业网站建设模板多少钱企业微信scrm
  • 查询建设银行卡卡号网站软文营销的技巧
  • 国外photoshop教程网站万网域名查询
  • 企业网站建站技术企业如何进行品牌推广
  • 建设工程的招标网站有哪些优化设计的答案
  • 酒店网站建设哪家好免费推广网站大全下载安装
  • 天津品牌网站建设如何制作一个网页页面
  • php动态网站开发简介站内优化seo
  • 做网站用什么语做网络推广好吗
  • 建设网站书线上推广平台都有哪些
  • 深圳网站建设推选上榜网络交换友情链接的方法
  • 一个网站建设10万元网站优化招商
  • 网站建站第十四课东莞建设企业网站
  • 魔鬼做交易网站最新新闻摘抄
  • 网站关键词优化工具渠道网
  • 广东建设行业招聘 什么网站seo培训资料
  • 免费建靓号网站seo数据分析
  • 珠海做网站国外免费域名申请
  • 手机房产网站模板360收录提交入口网址
  • 做淘宝客网站要多少钱广州抖音seo
  • html美食网页制作代码北京aso优化
  • 企业网站建设 哪个公司做得好西安抖音seo
  • 做电商平台网站太原seo关键词优化
  • 亚洲男女做暖网站推广引流方法与渠道