创建的话我们直接从FastThreadLocal的构造方法进入:
public FastThreadLocal() {
index = InternalThreadLocalMap.nextVariableIndex();
}
可见他是现在这里创建了index,这个index就是每个线程里面FastThreadLocal唯一的标识。
看这个nextVariableIndex()方法:
public static int nextVariableIndex() {
int index = nextIndex.getAndIncrement();
if (index < 0) {
nextIndex.decrementAndGet();
throw new IllegalStateException("too many thread-local indexed variables");
}
return index;
}
next是什么呢?
static final AtomicInteger nextIndex = new AtomicInteger();
所以没什么好说的,就是每次新建一个FastThreadLocal线程里面的index都会加1,这个index作为这个新建FastThreadLocal的标识,以后要找到这个FastThreadLocal就通过这个标识。
那么在什么地方找到这个FastThreadLocal呢?后面我们会知道,这里会有一个数组,可以通过FastThreadLocal的index找到这个FastThreadLocal对象在数组里面的位置。