美文网首页
ConcurrentHashMap.tableSizeFor(i

ConcurrentHashMap.tableSizeFor(i

作者: laosijikaichele | 来源:发表于2018-04-27 17:35 被阅读15次

    ConcurrentHashMap.tableSizeFor(int c)方法:

    private static final int tableSizeFor(int c) {
            int n = c - 1;
            n |= n >>> 1;
            n |= n >>> 2;
            n |= n >>> 4;
            n |= n >>> 8;
            n |= n >>> 16;
            return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
        }
    

    改进版:

    private static final int tableSizeFor(int c) {
            if(c <= 1) {return 1;}
            if(c > (MAXIMUM_CAPACITY >>> 1)) {return MAXIMUM_CAPACITY;}
            int n = c - 1;
            n |= n >>> 1;
            n |= n >>> 2;
            n |= n >>> 4;
            n |= n >>> 8;
            n |= n >>> 16;
            return n + 1;
        }
    

    相关文章

      网友评论

          本文标题:ConcurrentHashMap.tableSizeFor(i

          本文链接:https://www.haomeiwen.com/subject/serilftx.html