zoukankan      html  css  js  c++  java
  • ThreadLocalUtil 的使用

    package com.example.demo.util;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    public class ThreadLocalUtil<T> {
        private static final ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal() {
            @Override
            protected Map<String, Object> initialValue() {
                return new HashMap<>(4);
            }
        };
    
    
        public static Map<String, Object> getThreadLocal(){
            return threadLocal.get();
        }
    
        public static <T> T get(String key) {
            Map map = (Map)threadLocal.get();
            return (T)map.get(key);
        }
    
        public static <T> T get(String key,T defaultValue) {
            Map map = (Map)threadLocal.get();
            return (T)map.get(key) == null ? defaultValue : (T)map.get(key);
        }
    
        public static void set(String key, Object value) {
            Map map = (Map)threadLocal.get();
            map.put(key, value);
        }
    
        public static void set(Map<String, Object> keyValueMap) {
            Map map = (Map)threadLocal.get();
            map.putAll(keyValueMap);
        }
    
        public static void remove() {
            threadLocal.remove();
        }
    
        public static <T> Map<String,T> fetchVarsByPrefix(String prefix) {
            Map<String,T> vars = new HashMap<>();
            if( prefix == null ){
                return vars;
            }
            Map map = (Map)threadLocal.get();
            Set<Map.Entry> set = map.entrySet();
    
            for( Map.Entry entry : set){
                Object key = entry.getKey();
                if( key instanceof String ){
                    if( ((String) key).startsWith(prefix) ){
                        vars.put((String)key,(T)entry.getValue());
                    }
                }
            }
            return vars;
        }
    
        public static <T> T remove(String key) {
            Map map = (Map)threadLocal.get();
            return (T)map.remove(key);
        }
    
        public static void clear(String prefix) {
            if( prefix == null ){
                return;
            }
            Map map = (Map)threadLocal.get();
            Set<Map.Entry> set = map.entrySet();
            List<String> removeKeys = new ArrayList<>();
    
            for( Map.Entry entry : set ){
                Object key = entry.getKey();
                if( key instanceof String ){
                    if( ((String) key).startsWith(prefix) ){
                        removeKeys.add((String)key);
                    }
                }
            }
            for( String key : removeKeys ){
                map.remove(key);
            }
        }
    }

    使用的原理: https://www.cnblogs.com/WuXuanKun/p/5827060.html 

  • 相关阅读:
    7--SpringCloud:Config/Bus周阳老师
    6--SpringCloud:服务网关 gateway周阳老师
    5--SpringCloud:Hystrix 断路器周阳老师
    4--SpringCloud-Ribbon/OpenFeign周阳老师
    3--SpringCloud 和 Zookeeper周阳老师
    seata启动报错的可能原因,以及解决方案
    linux中Mysql的安装
    解决Nginx启动的一些问题
    svg配置与less在vue中使用
    微信小程序day01-基础认识到轮播图组件
  • 原文地址:https://www.cnblogs.com/hellohero55/p/13698195.html
Copyright © 2011-2022 走看看