power of two

学而时习之,不亦悦乎!

netty源码

public final class DefaultEventExecutorChooserFactory implements EventExecutorChooserFactory {

    public static final DefaultEventExecutorChooserFactory INSTANCE = new DefaultEventExecutorChooserFactory();

    private DefaultEventExecutorChooserFactory() { }

    @SuppressWarnings("unchecked")
    @Override
    public EventExecutorChooser newChooser(EventExecutor[] executors) {
        if (isPowerOfTwo(executors.length)) {
            return new PowerOfTwoEventExecutorChooser(executors);
        } else {
            return new GenericEventExecutorChooser(executors);
        }
    }
  //!!!判断一个数是否是2的倍数!!!
    private static boolean isPowerOfTwo(int val) {
        return (val & -val) == val;
    }

    private static final class PowerOfTwoEventExecutorChooser implements EventExecutorChooser {
        private final AtomicInteger idx = new AtomicInteger();
        private final EventExecutor[] executors;

        PowerOfTwoEventExecutorChooser(EventExecutor[] executors) {
            this.executors = executors;
        }

        @Override
        public EventExecutor next() {
            return executors[idx.getAndIncrement() & executors.length - 1];
        }
    }

    private static final class GenericEventExecutorChooser implements EventExecutorChooser {
        private final AtomicInteger idx = new AtomicInteger();
        private final EventExecutor[] executors;

        GenericEventExecutorChooser(EventExecutor[] executors) {
            this.executors = executors;
        }

        @Override
        public EventExecutor next() {
            return executors[Math.abs(idx.getAndIncrement() % executors.length)];
        }
    }
}

 HashMap

int一共4字节32位,2的倍数的特征就是二进制只有一个1.随意给定一个数,把它变成最近的2的倍数,其实只需要关注最高位的1,如果此时没有低位1,它就是2的倍数;如果有低位的1,那它的高一位1就是它最接近的2的倍数。例如00000100 00000000 00000000 00000000这个数,它就是2的倍数;例如00000111 00000000 11111111 11001100这个数,低位有1,那最接近的就是00001000 00000000 00000000 00000000.这里采用的算法是,拿到一个数,先减一,确保本身就是2的倍数不会变大一倍,然后把最高位的1向下传递,比如00000111 00000000 11111111 11001100变成00000111 11111111 11111111 11111111,这个时候加1,就变成了00001000 00000000 00000000 00000000。如果是00000100 00000000 00000000 00000000本身就是2的倍数,减1变成了00000011 11111111 11111111 11111111 11111111,把高位1传递后还是自己,加1变回了00000100 00000000 00000000 00000000。

最主要的原理就是只关心最高位的1.

    /**
     * Returns a power of two size for the given target capacity.
     */
    static final int tableSizeFor(int cap) {
        int n = cap - 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;
    }
原文地址:https://www.cnblogs.com/zby9527/p/12213032.html