Java ThreadLocal 静态的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16249687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:01:00  来源:igfitidea点击:

Java ThreadLocal static?

javamultithreadingjakarta-eethread-local

提问by Jasper

Setting a value in Thread Local:

在 Thread Local 中设置一个值:

//Class A holds the static ThreadLocal variable.

    Class A{

    public static ThreadLocal<X> myThreadLocal = new ThreadLocal<X>();             
    ....
    }


//A Class B method sets value in A's static ThreadLocal variable 
    class B{
    {
         public void someBmethod(){
             X x = new X();
             A.myThreadLocal.set(x);
         }
    }


//Class C retrieves the value set in A's Thread Local variable.

    Class C {

    public void someCMethod(){
         X x = A.myThreadLocal.get();
    }
    ...
    }

Quesiton:
Now assuming this is a web-application, and threads execute: B.someBMethod, C.someCMethod in that order.

问题
现在假设这是一个 Web 应用程序,并且线程按以下顺序执行:B.someBMethod、C.someCMethod。

Multiple threads executing B's someBMethod, will end up updating the SAMEA's static ThreadLocal variable myThreadLocal, thereby beating the very purpose of ThreadLocal variable. (Using static for ThreadLocal is what is recommended as per documentation.)

多个线程执行 B 的 someBMethod,最终将更新SAMEA 的静态 ThreadLocal 变量 myThreadLocal,从而违背了 ThreadLocal 变量的真正目的。(根据文档建议对 ThreadLocal 使用静态。)

The C's someCMethod, while retrieving value from ThreadLocal may not get the value set by the 'current' thread.

C 的 someCMethod,虽然从 ThreadLocal 检索值可能无法获得“当前”线程设置的值。

What am i missing here?

我在这里缺少什么?

采纳答案by sanbhat

As per the definition of ThreadLocalclass

根据ThreadLocal类的定义

This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable.ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID).

此类提供线程局部变量。这些变量不同于它们的普通对应变量,因为每个访问一个(通过其 get 或 set 方法)的线程都有自己的、独立初始化的变量副本。ThreadLocal 实例通常是希望将状态与线程相关联的类中的私有静态字段(例如,用户 ID 或事务 ID)。

That means say 2 threads t1& t2executes someBMethod()and they end up setting x1& x2(Instances of X) respectively. Now when t1comes and executes someCMethod()it gets x1(which is setby itselfearlier) and t2gets x2.

这意味着说 2 个线程t1&t2执行someBMethod(),它们最终分别设置x1& x2(Instances of X)。现在,当t1出现并执行someCMethod()它时,它会得到x1(它自己早先设置的)和得到。t2x2

In other words, its safe to have a single static instance of ThreadLocal, because internally it does something like this when you invoke set

换句话说,拥有 的单个静态实例是安全的ThreadLocal,因为当您调用它时,它在内部会执行类似的操作set

set(currentThread, value) //setting value against that particular thread

and when you invoke get

当你调用 get

get(currentThread) //getting value for the thread

回答by user207421

Multiple threads executing B's someBMethod, will end up updating the SAME A's static ThreadLocal variable myThreadLocal

多个线程执行 B 的 someBMethod,最终将更新 SAME A 的静态 ThreadLocal 变量 myThreadLocal

No they won't. Every thread has its own instance of the contained variable of type X.

不,他们不会。每个线程都有自己的 X 类型变量的实例。

thereby beating the very purpose of ThreadLocal variable

从而达到了 ThreadLocal 变量的目的

No.

不。

Have another look at the Javadoc.

再看看 Javadoc。

回答by NimChimpsky

No they won't, thats the point. Javadoc :

不,他们不会,这就是重点。文档:

These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID).

这些变量不同于它们的普通对应变量,因为每个访问一个(通过其 get 或 set 方法)的线程都有自己的、独立初始化的变量副本。ThreadLocal 实例通常是希望将状态与线程相关联的类中的私有静态字段(例如,用户 ID 或事务 ID)。

回答by NPE

Multiple threads executing B's someBMethod, will end up updating the SAME A's static ThreadLocal variable myThreadLocal

多个线程执行 B 的 someBMethod,最终将更新 SAME A 的静态 ThreadLocal 变量 myThreadLocal

Yes, they operate on the same object. However, it is important to realize that the way ThreadLocalworks is that each thread has its own, separate value. Thus if you have ten threads writing to myThreadLocaland then reading from myThreadLocal, each will see the correct (i.e. their own) value.

是的,它们对同一个对象进行操作。但是,重要的是要意识到ThreadLocal工作方式是每个线程都有自己的独立值。因此,如果您有十个线程写入myThreadLocal然后读取myThreadLocal,每个线程都会看到正确的(即它们自己的)值。

To put it another way, it does not matter which class or object writes to an instance of ThreadLocal. What matters is the threadin whose context the operation is performed.

换句话说,哪个类或对象写入ThreadLocal. 重要的是在其上下文中执行操作的线程

回答by Sundararaj Govindasamy

I study the java source code,

我研究了java源代码

  1. java.lang.Thread Classcontains a instancevariable as below.

    ThreadLocal.ThreadLocalMap threadLocals = null;

  1. java.lang.Thread Class包含一个实例变量,如下所示。

    ThreadLocal.ThreadLocalMap threadLocals = null;

Because threadLocalsvariable is non-static, Every thread in a application (i.e., every instance of Thread Class) will have it's own copyof threadLocals map.

由于threadLocals变量是非静态的,应用程序中的每个线程(即Thread Class 的每个实例)都将拥有自己的 threadLocals 映射副本

  1. Keyfor this map is, currentThreadLocal instance, and valueis the value which you pass as argumentto ThreadLocal.set().

  2. When you try to fetchvalue as ThreadLocal.get(), internally, it will fetch from the ThreadLocalMap of Current Thread.

  1. 此映射的当前ThreadLocal 实例,是您作为参数传递给 ThreadLocal.set() 的值。

  2. 当您尝试以as获取值时ThreadLocal.get(),它会在内部从Current Thread的 ThreadLocalMap 中获取。

In simple terms, you are getting & setting values from/to your current ThreadObject, notfrom/to your ThreadLocalobject.

简单来说,您是从/到当前Thread对象获取和设置值,而不是从/到您的ThreadLocal对象。