简体   繁体   English

ThreadLocal的目的是什么?

[英]What's the purpose of ThreadLocal here?

public class VPattern implements Pattern
{
    private final TokenKey tokenKey_;
    private final String tokenLabel_;
    private Integer cachedHashCode_ = null;
    private ThreadLocal<Token> token_ = new ThreadLocal<Token>();

    ...
}

I am reading this piece of code and don't understand the use of ThreadLocal here. 我正在阅读这段代码,并且在这里不了解ThreadLocal的用法。 Is that because ThreadLocal is used to ensure the 'token_' object will be thread safe in any concurrent situation? 那是因为ThreadLocal用于确保'token_'对象在任何并发情况下都是线程安全的吗? If that's the case, why TokenKey and Integer are not thread safety protected? 如果是这样,为什么TokenKey和Integer不受线程安全保护? I know that "String" is always thread safe. 我知道“字符串”始终是线程安全的。

Every thread gets its own Token even if they share the same instance of VPattern . 即使每个线程共享相同的VPattern实例,每个线程也会获得自己的Token Possibly this was done because Token is not thread safe and VPattern wants to avoid synchronizing access to the Token instance. 之所以这样做是因为Token不是线程安全的,并且VPattern希望避免同步对Token实例的访问。 tokenKey_ is final so don't have to worry about the field changing, and maybe it's thread safe on its own. tokenKey_是最终的,因此不必担心字段更改,并且它本身可能是线程安全的。 tokenLabel_ is also final and strings are immutable so no issue there. tokenLabel_也是最终的,字符串是不可变的,因此在那里没有问题。 cachedHashCode_ is the odd one out here; cachedHashCode_是这里的奇数; is access to it protected somehow? 对它的访问受到某种保护吗? It's hard to say what's going on without seeing the rest of the class. 在不看课程其余部分的情况下很难说发生了什么。

In general, ThreadLocal can provide different objects for each working thread. 通常, ThreadLocal可以为每个工作线程提供不同的对象。 So if the given object is not thread-safe nor singleton, it can be stored in ThreadLocal variable. 因此,如果给定的对象不是线程安全的也不是单例的,则可以将其存储在ThreadLocal变量中。 Then every thread may get and safely use different instance of your class. 然后,每个线程都可以安全地使用类的不同实例。 You can treat it like a map, where the current thread is a key and the actual object is a value. 您可以将其视为地图,其中当前线程是键,而实际对象是值。

Let's assume there are two threads working at the same time and sharing one VPattern object. 假设有两个线程同时工作并共享一个VPattern对象。 If threads get tokenKey_ or tokenLabel_ , then both will get the same instances. 如果线程获得tokenKey_tokenLabel_ ,则两者将获得相同的实例。 But if both threads call token.get() , then they will get different instances of Token type (if initialized previously, see: set method and withInitial static factory method). 但是,如果两个线程都调用token.get() ,则它们将获得Token类型的不同实例(如果先前已初始化,请参见: set方法和withInitial静态工厂方法)。

Unfortunately it's hard to say what's the purpose of ThreadLocal in your case because it highly depends on the context. 不幸的是,很难说ThreadLocal的目的是什么,因为它高度依赖于上下文。 Seems that Token objects cannot be shared by different threads (each thread should have own token). 似乎Token对象不能由不同的线程共享(每个线程应具有自己的令牌)。

You can read more about ThreadLocal in javadoc or here: https://www.baeldung.com/java-threadlocal 您可以在javadoc或此处阅读有关ThreadLocal更多信息: https : //www.baeldung.com/java-threadlocal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM