并发编程系列——3ThreadLocal核心原理分析

ThreadLocal是啥?ThreadLocal是干啥用的?ThreadLocal底层数据结构是什么?ThreadLocal如何解决hash冲突的?java中存在哪几种引用?ThreadLocal中的key是什么引用?为什么要用这种引用?ThreadLocal怎么解决内存泄漏的?
摘要由CSDN通过智能技术生成

学习目标

  1. ThreadLocal是啥

  2. ThreadLocal是干啥用的

  3. ThreadLocal底层数据结构是什么

  4. ThreadLocal如何解决hash冲突的

  5. java中存在哪几种引用

  6. ThreadLocal中的key是什么引用,为什么要用这种引用

  7. ThreadLocal怎么解决内存泄漏的

第1章 使用场景

作用:ThreadLocal 用作保存每个线程独享的对象,为每个线程都创建一个副本,这样每个线程都可以修改自己所拥有的副本, 而不会影响其他线程的副本,确保了线程安全(线程隔离机制)

 

1.1 场景1

这种场景通常用于保存线程不安全的工具类,典型的需要使用的类就是 SimpleDateFormat

在这种情况下,每个Thread内都有自己的实例副本,且该副本只能由当前Thread访问到并使用,相当于每个线程内部的本地变量,这也是ThreadLocal命名的含义。因为每个线程独享副本,而不是公用的,所以不存在多线程间共享的问题。

//每个线程都创建了SimpleDateFormat对象
public class ThreadLocalDemo01 {
    //这里不会出现线程不安全的问题
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            int s = i;
            new Thread(()->{
                String data = new ThreadLocalDemo01().date(s);
                System.out.println(data);
            }).start();
            Thread.sleep(100);
        }
    }

    private String date(int seconds){
        Date date = new Date(1000*seconds);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");
        return simpleDateFormat.format(date);
    }
}

//每次线程调用也是单独创建SimpleDateFormat对象
public class ThreadLocalDemo02 {
    public static ExecutorService threadPool = Executors.newFixedThreadPool(16);
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            int s = i;
            threadPool.submit(()->{
                String data = new ThreadLocalDemo02().date(s);
                System.out.println(data);
            });
        }
        threadPool.shutdown();
    }

    private String date(int seconds){
        Date date = new Date(1000*seconds);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");
        return simpleDateFormat.format(date);
    }
}

//出现线程安全问题了,共享一个SimpleDateFormat对象
public class ThreadLocalDemo03 {
    public static ExecutorService threadPool = Executors.newFixedThreadPool(16);
    public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            int s = i;
            threadPool.submit(()->{
                String data = new ThreadLocalDemo03().date(s,simpleDateFormat);
                System.out.println(data);
            });
        }
        threadPool.shutdown();
    }

    private String date(int seconds,SimpleDateFormat simpleDateFormat){
        Date date = new Date(1000*seconds);

        return simpleDateFormat.format(date);
    }
}

//使用ThreadLocal做隔离
public class ThreadLocalDemo04 {
    public static ExecutorService threadPool = Executors.newFixedThreadPool(16);
    public static ThreadLocal<SimpleDateFormat> dateFormatThreadLocal = ThreadLocal.withInitial(() -> new SimpleDateFormat("mm:ss"));
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            int s = i;
            threadPool.submit(()->{
                String data = new ThreadLocalDemo04().date(s);
                System.out.println(data);
            });
        }
        threadPool.shutdown();
    }

    private String date(int seconds){
        Date date = new Date(1000*seconds);
        SimpleDateFormat dateFormat = dateFormatThreadLocal.get();
        return dateFormat.format(date);
    }
}

在ThreadLocalDemo01中们给每个线程都创建了SimpleDateFormat对象,他们之间互不影响,代码是可以正常执行的。

在ThreadLocalDemo02中可以看出,我们用了一个16线程的线程池

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木木_2024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值