ThreadLocal

这样使用 ThreadLocal 对吗?

 

最近我看到同事再用 ThreadLocal,他没用出想要的效果就来咨询我,然后我大概解释了一下,然后连我自己都糊涂了。所以趁机又看了一下《深入理解java虚拟机》这本书,下面说一说我个人对 ThreadLocal 的理解,解释有误的地方请留言指正!

ThreadLocal

要理解 ThreadLocal,先来看看官方对它的解释:

This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its {@code get} or {@code set} method) has its own, independently initialized copy of the variable. {@code 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).

我翻译一下,大概意思是这样(可能翻译的不是很准确,见谅):

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

总结一下重点:

  • ThreadLocal 提供了一种访问某个变量的特殊方式:访问到的变量属于当前线程,即保证每个线程的变量不一样,而同一个线程在任何地方拿到的变量都是当前这个线程私有的,这就是所谓的线程隔离。
  • 如果要使用 ThreadLocal,通常定义为 private static 类型,在我看来最好是定义为 private static final 类型。

理解了 ThreadLocal,我们来看 ThreadLocal 的使用场景:

对同一个线程调用的多个方法中,共享了某一个变量,这个变量需要传递到多个方法中,这样传来传去太麻烦了,这时就可以采用 ThreadLocal 了。

下面我们来看一个错误的用法。先创建一个用来作为共享变量的实体类:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

package com.xttblog.test;

 

public class Xttblog {

    private String title;

    private Long id;

    private String text;

 

    public String getTitle() {

        return title;

    }

 

    public void setTitle(String title) {

        this.title = title;

    }

 

    public Long getId() {

        return id;

    }

 

    public void setId(Long id) {

        this.id = id;

    }

 

    public String getText() {

        return text;

    }

 

    public void setText(String text) {

        this.text = text;

    }

 

    public Xttblog(String title, Long id, String text) {

        this.title = title;

        this.id = id;

        this.text = text;

    }

}

然后创建一个线程 ThreadLocalTarget:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

package com.xttblog.test;

 

public class ThreadLocalTarget implements Runnable{

    private static Xttblog blog = new Xttblog("ThreadLocal 会导致内存泄露?",1L,"....");

    public static ThreadLocal<Xttblog> local = new ThreadLocal<Xttblog>();

    public ThreadLocalTarget(){}

 

    @Override

    public void run() {

        ThreadLocalTarget.local.set(blog);

 

        ThreadLocalTarget.local.get().setTitle("test");

 

        System.out.println(Thread.currentThread().getName() + "更改后的名字:" + ThreadLocalTarget.local.get().getTitle());

    }

 

    public static Xttblog getBlog() {

        return blog;

    }

}

然后模仿这个线程在实际中的应用:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

package com.xttblog.test;

 

public class ThreadLocalTest {

    public static void main(String[] args) throws InterruptedException {

        ThreadLocalTarget target = new ThreadLocalTarget();

 

        Thread thread = new Thread(target);

 

        thread.start();

        thread.join();

 

        System.out.println(Thread.currentThread().getName() + "更改后的名字:" + ThreadLocalTarget.getBlog().getTitle());

    }

}

运行 ThreadLocalTest 之后,你会傻眼了。ThreadLocal 不是共享变量,是拷贝的变量的一个副本吗?怎么变量的值最终被改变了。

1

2

Thread-0更改后的名字:test

main更改后的名字:test

这不是 ThreadLocal 的错,是你没理解她。

我前面说过 ThreadLocal 保证的是同一个线程内部调用的各种方法共享当前线程中 ThreadLocal 的变量。就是说针对同一个线程中任何地方访问属于当前现在的共享变量是同一个。从上面也可以看出 ThreadLocal 不是拷贝的变量的副本。

还没明白(也许是我没说清楚),我们来看看阿里巴巴 java 开发手册中推荐的 ThreadLocal 的用法:

1

2

3

4

5

6

7

8

9

10

11

12

13

package com.xttblog.test;

 

import java.text.DateFormat;

import java.text.SimpleDateFormat;

 

public class DateUtil {

    public static final ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>(){

        @Override

        protected DateFormat initialValue() {

            return new SimpleDateFormat("yyyy-MM-dd");

        }

    };

}

然后我们再要用到 DateFormat 对象的地方,这样调用:

1

DateUtils.df.get().format(new Date());

如果还没明白,我再解释一下,ThreadLocal 相当于每个线程A在创建的时候,已经为你创建好了一个 DateFormat,这个 DateFormat 在当前这个线程A中共享。其他线程B,再用到 DateFormat 的地方,也会创建一个 DateFormat 对象,这个对象会在线程 B 中共享,直到线程 B 结束。

也就是说 ThreadLocal 的用法和我们自己 new 对象一样,然后将这个 new 的对象传递到各个方法中。但是到处传递的话,太麻烦了。这个时候,就应该用 ThreadLocal。

总结:ThreadLocal 并不是为了解决线程安全问题,而是提供了一种将实例绑定到当前线程的机制,类似于隔离的效果,实际上自己在方法中 new 出来变量也能达到类似的效果。ThreadLocal 跟线程安全基本不搭边,绑定上去的实例也不是多线程公用的,而是每个线程 new 一份,这个实例肯定不是共用的,如果共用了,那就会引发线程安全问题。ThreadLocal 最大的用处就是用来把实例变量共享成全局变量,在程序的任何方法中都可以访问到该实例变量而已。网上很多人说 ThreadLocal 是解决了线程安全问题,其实是望文生义,两者不是同类问题

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值