java.lang.Thread
类新增了3个字段:/** The current seed for a ThreadLocalRandom */
@sun.misc.Contended("tlr")
long threadLocalRandomSeed;
/** Probe hash value; nonzero if threadLocalRandomSeed initialized */
@sun.misc.Contended("tlr")
int threadLocalRandomProbe;
/** Secondary seed isolated from public ThreadLocalRandom sequence */
@sun.misc.Contended("tlr")
int threadLocalRandomSecondarySeed;
正如Javadoc中所述,这些变量仅由类java.util.concurrent.ThreadLocalRandom
进行管理。
此外,在ThreadLocalRandom
中,它们被用于非常奇特的方式:
SEED = UNSAFE.objectFieldOffset
(tk.getDeclaredField("threadLocalRandomSeed"));
PROBE = UNSAFE.objectFieldOffset
(tk.getDeclaredField("threadLocalRandomProbe"));
SECONDARY = UNSAFE.objectFieldOffset
(tk.getDeclaredField("threadLocalRandomSecondarySeed"));
同样的代码片段也可以在LockSupport
类中找到。
然后这些偏移量在几个java.concurrent
的地方被内部使用。
这个想法是什么?为什么这些字段放在java.lang.Thread
内部?为什么不放在ThreadLocalRandom
中?