本文介绍了java.lang.Thread中的新增附加字段,有什么想法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java 8中, java.lang.Thread 类有3个新字段:

In Java 8, java.lang.Thread class got 3 new fields:

/** 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

as it said in Javadoc for being exclusively managed by class java.util.concurrent.ThreadLocalRandom.

此外,在 ThreadLocalRandom 中,它们以非常奇特的方式使用:

Furthermore, in ThreadLocalRandom they are used in very freakish way:

SEED = UNSAFE.objectFieldOffset
    (tk.getDeclaredField("threadLocalRandomSeed"));
PROBE = UNSAFE.objectFieldOffset
    (tk.getDeclaredField("threadLocalRandomProbe"));
SECONDARY = UNSAFE.objectFieldOffset
    (tk.getDeclaredField("threadLocalRandomSecondarySeed"));

(同样的代码片段也可以在 LockSupport class)。

(the same code piece can be met also in LockSupport class).

然后这些偏移在内部用于几个 java.concurrent 地方。

and then this offsets are used internally in several java.concurrent places.

这是什么意思?为什么这些字段位于 java.lang.Thread 中?为什么不在 ThreadLocalRandom

What is the idea? Why these fields are places inside java.lang.Thread? Why not inside ThreadLocalRandom?

推荐答案

这些是内部字段。解释只能来自JDK开发人员自己。我能找到一个关于这一点来自2013年1月的Doug Lea,它解释了这些字段背后的基本原理以及为什么它们位于 Thread 类中。

These are internal fields. Explanations can only come from JDK developers themselves. I was able to find a post about this from Doug Lea dated January 2013, that explains the rationale behind those fields and why they are inside the Thread class.

所以我建议将以下内容添加到类Thread:

So I propose adding the following to class Thread:

// The following three initially uninitialized fields are exclusively
// managed by class java.util.concurrent.ThreadLocalRandom.
/** The current seed for a ThreadLocalRandom */
long threadLocalRandomSeed;
/** Probe hash value; nonzero if threadLocalRandomSeed initialized */
int threadLocalRandomProbe;
/** Secondary seed isolated from public ThreadLocalRandom sequence */
int threadLocalRandomSecondarySeed;

这样做的原因是:


  1. 以更快的速度访问 ThreadLocalRandom 状态。虽然
    ThreadLocal 访问通常已经非常快,但这不仅是更快的
    ,在用户
    程序创建的情况下不会降低大量的 ThreadLocal s,其中
    可能(概率上)导致任何给定的访问速度变慢

  1. Uniformly faster access to ThreadLocalRandom state. While ThreadLocal access is normally pretty fast already, this is not only faster, it does not degrade in cases where user programs create large numbers of ThreadLocals, which may (probabilistically) cause any given access to become slower.

使用 ThreadLocalRandom 的任何程序的总占用空间更小。
三个字段比装入填充的
ThreadLocal 对象所需的空间更少。由于 ThreadLocalRandom 在JDK本身中被广泛使用
,这几乎包括所有程序。

Smaller total footprint for any program using ThreadLocalRandom. Three fields require less space than boxing into a padded ThreadLocal object. As ThreadLocalRandom becomes widely used within JDK itself, this includes just about all programs.

java.util.concurrent ForkJoinPool
ConcurrentHashMap ,<$ c $进一步节省时间/空间c> LongAdder
ConcurrentSkipList ,以及其他
类,可以使用这种形式的统一 ThreadLocalRandom
簿记而不是他们自己的特殊目的 ThreadLocal s
他们现在这样做。

Further time/space savings for java.util.concurrent ForkJoinPool, ConcurrentHashMap, LongAdder, ConcurrentSkipList, and other classes that could use this form of the unified ThreadLocalRandom bookkeeping rather than their own special-purpose ThreadLocals as they now do.


这篇关于java.lang.Thread中的新增附加字段,有什么想法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 14:42