小狼-ThreadLocal基础使用

本文深入探讨了ThreadLocal的工作原理及使用方式,并通过具体实例展示了如何在多线程环境中使用ThreadLocal来隔离线程数据,同时介绍了InheritableThreadLocal的特点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.aynu.util;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaolang
 * @description: ThreadLocalUtil
 * @date 2022/3/25 14:49
 */
@Data
public class ThreadLocalUtil {

    private static ThreadLocal<List<String>> thread1 = new ThreadLocal<List<String>>() {
        @Override
        protected synchronized List<String> initialValue() {
            return new ArrayList<>();
        }
    };

    private static ThreadLocal<List<String>> thread2 = new ThreadLocal<List<String>>() {
        @Override
        protected synchronized List<String> initialValue() {
            return new ArrayList<>();
        }
    };

    /**
     * InheritableThreadLocal可以让子线程获取到父线程的ThreadLocal
     */
    private static ThreadLocal<List<String>> thread3 = new InheritableThreadLocal<List<String>>() {
        @Override
        protected synchronized List<String> initialValue() {
            return new ArrayList<>();
        }
    };

    public static List<String> getThread1() {
        return thread1.get();
    }

    public static void setThread1(List<String> list) {
        thread1.set(list);
    }

    public static List<String> getThread2() {
        return thread2.get();
    }

    public static void setThread2(List<String> list) {
        thread2.set(list);
    }

    public static List<String> getThread3() {
        return thread3.get();
    }

    public static void setThread3(List<String> list) {
        thread3.set(list);
    }

    public static void main(String[] args) throws InterruptedException {
        ArrayList<String> list = new ArrayList<>();
        list.add("Thread1");
        ThreadLocalUtil.setThread1(list);
        List<String> thread1 = ThreadLocalUtil.getThread1();
        System.out.println("父线程Thread1 = " + thread1);
        new Thread(() -> {
            List<String> childTradeDataMap = ThreadLocalUtil.getThread1();
            System.out.println("子线程tradeDataMap = " + childTradeDataMap);
        }).start();
        Thread.sleep(2000);
        System.out.println("================================== = ");
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("Thread2");
        ThreadLocalUtil.setThread2(list2);
        List<String> thread2 = ThreadLocalUtil.getThread2();
        System.out.println("thread2 = " + thread2);

        System.out.println("父线程Thread1 = " + ThreadLocalUtil.getThread1());

        System.out.println("================================== = ");
        ArrayList<String> list3 = new ArrayList<>();
        list3.add("Thread3");
        ThreadLocalUtil.setThread3(list3);
        List<String> thread3 = ThreadLocalUtil.getThread3();
        System.out.println("父线程thread3 = " + thread3);
        new Thread(() -> {
            List<String> thread31 = ThreadLocalUtil.getThread3();
            System.out.println("子线程thread3 = " + thread31);
        }).start();
        Thread.sleep(2000);
    }
}

要点:

1.每个线程有自己的ThreadLocal

2.子线程可以拿到父线程的ThreadLocal

3.用完要remove一下

例子:

在controller中先设置再获取 

 有的小伙伴就要问了,哎呀,我根本拿不到值,你这不是坑人吗?

每个人进来请求都是单独的一个线程,我们稍微设置一下很容易就看到我们设置的值了

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值