Java ThreadLocal static?

前端 未结 5 966
独厮守ぢ
独厮守ぢ 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:23

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

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

    thereby beating the very purpose of ThreadLocal variable

    No.

    Have another look at the Javadoc.

提交回复
热议问题