ThreadLocal使用

场景:同一个线程,不同的类之间的变量传递。

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);
    }

}

上一篇:iOS Masonry 布局- UIScrollView/Masonry自动布局对UIScrollView的内容自适应


下一篇:Vue 监听某个元素滚动,亲测有效