springboot 使用threadlocal

概念

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

工具类

public class BaseContext {  
  
public static ThreadLocal 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();  
  }  
  
}