ThreadLocal学习

霸气de小男生 提交于 2020-01-10 15:37:14
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadLocalTest {

    private static AtomicInteger nextHashCode = new AtomicInteger();

    //https://www.cnblogs.com/ilellen/p/4135266.html
    //https://www.jianshu.com/p/640f2c0ac4b0
    //https://www.jianshu.com/p/cdb2ea3792b5
    private static final int HASH_INCREMENT = 0x61c88647;
    private static final int INITIAL_CAPACITY = 16;

    public static void main(String[] args){
        for(int i = 0; i < INITIAL_CAPACITY; i++){
            System.out.println(getIndex());
        }
    }

    private static int nextHashCode() {
        int hashCode = nextHashCode.getAndAdd(HASH_INCREMENT);
        System.out.print(String.format("hashCode: 0x%x \t index: " , hashCode));
        return hashCode;
    }

    private static int getIndex(){
        return nextHashCode() & (INITIAL_CAPACITY - 1);
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!