场景:同一个线程,不同的类之间的变量传递。
public class StrokeUtil {
private static ThreadLocal<ConcurrentHashMap<String, Long>> strokeThreadLocal = new ThreadLocal<>();
public static void remove(){
strokeThreadLocal.remove();
}
public static Long getIdByName(String name){
if (Objects.isNull(strokeThreadLocal.get())){
strokeThreadLocal.set(new ConcurrentHashMap<>(64));
}
return strokeThreadLocal.get().get(name);
}
public static void addData(String name, Long id){
strokeThreadLocal.get().put(name, id);
}
}