Java ThreadLocal static?

前端 未结 5 959
独厮守ぢ
独厮守ぢ 2020-12-23 10:43

Setting a value in Thread Local:

//Class A holds the static ThreadLocal variable.

    Class A{

    public static ThreadLocal myThreadLocal = new T         


        
5条回答
  •  暖寄归人
    2020-12-23 11:16

    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).

提交回复
热议问题