springboot 使用threadlocal

概念

threadlocal为每一个线程提供一个单独的存储空间,具有线程隔离的作用,只有在线程内才能获取到对应的值,线程外则不能访问

工具类

csharp 复制代码
public class BaseContext {  
  
public static ThreadLocal<Long> threadLocal = new ThreadLocal<>();  
  
public static void setCurrentId(Long id) {  
    threadLocal.set(id);  
  }  
  
public static Long getCurrentId() {  
    return threadLocal.get();  
  }  
  
public static void removeCurrentId() {  
    threadLocal.remove();  
  }  
  
}
相关推荐
henujolly22 分钟前
go学习day two
后端
AI袋鼠帝35 分钟前
腾讯这只小程序Agent🦀,帮我找到了最强日程、文件、知识管理姿势
后端
努力的小郑43 分钟前
突发!Claude Code 51万行源码全网裸奔:一场史诗级“开源”事故,国内大厂笑麻了
前端·后端·ai编程
HashTang1 小时前
Claude Code 源码中 REPL.tsx 深度解析:一个 5005 行 React 组件的架构启示
前端·后端·ai编程
thatway19892 小时前
ARM TFM-1介绍及代码下载运行适配
后端
千寻girling2 小时前
不知道 Java 全栈 + AI 编程有没有搞头 ?
前端·人工智能·后端
小码哥_常2 小时前
Spring Boot 实现网络限速:让流量“收放自如”
后端
johnrui3 小时前
SpringBoot-JdbcTemplate
java·spring boot·后端
Victor3564 小时前
MongoDB(72)如何创建用户和角色?
后端
Victor3564 小时前
MongoDB(71)如何启用MongoDB身份验证?
后端