ConcurrentHashMap中的2的n次方幂上舍入方法
生活随笔
收集整理的這篇文章主要介紹了
ConcurrentHashMap中的2的n次方幂上舍入方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近看JDK中的concurrentHashMap類的源碼,其中有那么一個函數:
/*** Returns a power of two table size for the given desired capacity.* See Hackers Delight, sec 3.2*/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;}看不明白這是做什么的,但是后來我查看了《hackers delight》(中譯:高效程序的奧秘)這本書,才知道這個函數是用來計算c的上舍入到2的n次冪。書上的函數如下圖:
?
My Github
轉載于:https://www.cnblogs.com/katsura/p/4134737.html
總結
以上是生活随笔為你收集整理的ConcurrentHashMap中的2的n次方幂上舍入方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于 XIB 和 NIB
- 下一篇: WCF服务实现客户端Cookie共享,表