码迷,mamicode.com
首页 > 其他好文 > 详细

power of two

时间:2020-01-19 12:35:39      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:ble   res   tables   一个   传递   ret   final   atom   table   

学而时习之,不亦悦乎!

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;
    }

power of two

标签:ble   res   tables   一个   传递   ret   final   atom   table   

原文地址:https://www.cnblogs.com/zby9527/p/12213032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!