[英]Why design ThreadLocalMap as static nest class in ThreadLocal?
Why design ThreadLocalMap
as Static Class in ThreadLocal
? 为什么设计
ThreadLocalMap
作为静态类中ThreadLocal
? While static and non-static nested classes have differences below. 静态和非静态嵌套类在下面有区别。
Nested static class doesn't need reference of Outer class, but Non-static nested class or Inner class requires Outer class reference. 嵌套的静态类不需要外部类的引用,但是非静态的嵌套类或内部类需要外部类的引用。
Inner class(or non-static nested class) can access both static and non-static members of Outer class. 内部类(或非静态嵌套类)可以访问外部类的静态和非静态成员。 A static class cannot access non-static members of the Outer class.
静态类无法访问Outer类的非静态成员。 It can access only static members of Outer class.
它只能访问Outer类的静态成员。
An instance of Inner class cannot be created without an instance of outer class and an Inner class can reference data and methods defined in Outer class in which it nests, so we don't need to pass reference of an object to the constructor of the Inner class. 没有外部类的实例就不能创建内部类的实例,并且内部类可以引用嵌套在外部类中的数据和方法,因此我们不需要将对象的引用传递给内部类的构造函数类。 For this reason Inner classes can make program simple and concise.
因此,内部类可以使程序简单明了。
First it should be noted that ThreadLocalMap
is a package-private class, thus it's not a part of the API, but an implementation detail which may change in future JDK versions if necessary. 首先应该注意,
ThreadLocalMap
是一个程序包专用类,因此它不是API的一部分,而是实现的详细信息,如有必要,将来的JDK版本中可能会更改该实现的详细信息。
Why it's not non-static? 为什么它不是非静态的? Just because non-static nested class (inner class) is bound to the concrete instance of the outer class.
仅仅因为非静态嵌套类(内部类)绑定到外部类的具体实例。 In our case it should be bound to the concrete
ThreadLocal
variable. 在我们的例子中,它应该绑定到具体的
ThreadLocal
变量。 This is just wrong: ThreadLocalMap
is bounded to the thread and stores all the values of all ThreadLocal
variables for given thread. 这是错误的:
ThreadLocalMap
绑定到线程并存储给定线程的所有ThreadLocal
变量的所有值。 Thus binding it to the concrete ThreadLocal
instance is meaningless. 因此,将其绑定到具体的
ThreadLocal
实例是没有意义的。
Maybe it looks more logical to make ThreadLocalMap
the inner class of the Thread
class. 使
ThreadLocalMap
成为Thread
类的内部类似乎更合乎逻辑。 However ThreadLocalMap
just does not need the Thread
object to operate, so this would just add some unnecessary overhead. 但是
ThreadLocalMap
只是不需要Thread
对象来操作,因此这只会增加一些不必要的开销。 The reason it's located inside the ThreadLocal
class is that the ThreadLocal
is responsible for ThreadLocalMap
creation. 它位于
ThreadLocal
类内的原因是ThreadLocal
负责ThreadLocalMap
创建。 ThreadLocalMap
is created for current thread only when the first ThreadLocal
is set in this thread, but after that the same ThreadLocalMap
is used for all other ThreadLocal
variables. ThreadLocalMap
为只有当第一电流线程创建ThreadLocal
在这个线程设置,但在此之后同样ThreadLocalMap
用于所有其他ThreadLocal
变量。
Another alternative would be to hold ThreadLocalMap
as separate package-private class in java.lang
package. 另一种选择是将
ThreadLocalMap
作为java.lang
包中的单独的package-private类保存。 Nothing wrong with such option: it's just a matter of taste and depends on what developers want more: to group related functionality in single source file or to keep source file length smaller. 这样的选项没什么不对:这只是一个问题,取决于开发人员的需求:将相关功能组合在单个源文件中或保持源文件的长度较小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.