前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >sun.misc.Hashing cannot be resolved to a type

sun.misc.Hashing cannot be resolved to a type

作者头像
johnhuster的分享
发布2022-03-28 20:16:18
3070
发布2022-03-28 20:16:18
举报
文章被收录于专栏:johnhuster

sun.*包内的类在开发时尽量不要使用,官网上也建议大家不要使用,可以参考https://www.oracle.com/java/technologies/faq-sun-packages.html,像eclipse之类的编译器也会对引用sun.*子包内的类给出编译错误,要想使用必须相应设置可以绕过这一限制,但是这却不是一个好的开发习惯,sun.*子包内的类很可能会在后续版本中移除,sun.misc.Hashing这个类在jdk8中就移除了,笔者为了看下jdk7中hashmap用在多线程情况下死循环的问题,将jdk7中hashmap实现拷贝出来,但是在编译时报了“sun.misc.Hashing cannot be resolved to a type”编译失败,在网上搜了很多,基本上没人提到这个问题,大部分都是sun.misc.Base64Encoder、sun.misc.Base64Decoder之类的信息,最终在eclipse使用CTRL+SHIFT+T查了下Hashing这个类,发现这个类在jdk7版本中存在,但在jdk8版本中却移除了。

PS:

1、为了代码的可移植性,尽量慎用sun包下类

2、JDK7中hashmap用在多线程中出现死循环问题就在于扩容时数据重新配置时逆序导致的,只需要将transfer方法进行下面的调整就可以避免死循环问题,这也是jdk8中数据重新分配策略:

代码语言:javascript
复制
    /**
     * Transfers all entries from current table to newTable.
     */
    void transfer(Entry[] newTable, boolean rehash) {
           //jdk7中调整策略,用在多线程中存在死循环问题
/*        int newCapacity = newTable.length;
        for (Entry<K,V> e : table) {
            while(null != e) {
                Entry<K,V> next = e.next;
                if (rehash) {
                    e.hash = null == e.key ? 0 : hash(e.key);
                }
                int i = indexFor(e.hash, newCapacity);  //所在散列桶序号
                e.next = newTable[i];   //这边做了逆序处理,在多线程使用时会导致死循环
                newTable[i] = e;
                e = next;
            }
        }*/
        int oldCap = table.length;
        for (Entry<K,V> e : table) {
        	Entry<K,V> loHead = null, loTail = null;
        	Entry<K,V> hiHead = null, hiTail = null;
        	int i =0;
            while(null != e) {
                Entry<K,V> next = e.next;
                if (rehash) {
                    e.hash = null == e.key ? 0 : hash(e.key);
                }
                i = e.hash & (oldCap-1);
                
                if ((e.hash & oldCap) == 0) {//在原来的散列桶
                    if (loTail == null)
                        loHead = e;
                    else
                        loTail.next = e;
                    loTail = e;
                }
                else {//不在原来的散列桶
                    if (hiTail == null)
                        hiHead = e;
                    else
                        hiTail.next = e;
                    hiTail = e;
                }  
                e = next;
            }
            if (loTail != null) {
                loTail.next = null;
                newTable[i] = loHead;
            }
            if (hiTail != null) {
                hiTail.next = null;
                newTable[i + oldCap] = hiHead;
            }            
        }
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/12/11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档