zoukankan      html  css  js  c++  java
  • ThreadLocal 定义、使用场景、案例、原理、注意事项

    (一)定义

    This class provides thread-local variables.

    线程本地变量,线程独有的变量,作用域为当前线程

    (二)使用场景

    (1) 目标变量只与当前线程有关,每个线程需要有该值的备份

    (2) 目标变量在线程执行过程中多次使用,导致需要在每个用到的方法都要作为参数传递,ThreadLocal提供了一种从当前线程取变量的途径

    (三)案例

    (1)Java文档提供的官方案例 -- 线程独有的标识符

     ThreadId类提供了每个线程的id,用于标识该线程,通过ThreadId.get()方法获取该标识

    通过使用ThreadLocal,程序就可以根据当前线程取查找对应的id标识

     public class ThreadId {
          // Atomic integer containing the next thread ID to be assigned
          private static final AtomicInteger nextId = new AtomicInteger(0);
     
          // Thread local variable containing each thread's ID
          private static final ThreadLocal<Integer> threadId =
              new ThreadLocal<Integer>() {
                  @Override protected Integer initialValue() {
                      return nextId.getAndIncrement();
              }
          };
     
          // Returns the current thread's unique ID, assigning it if necessary
          public static int get() {
              return threadId.get();
          }
      }

    (2)fastjson中分配字节空间 - JSON.bytesLocal

    在这个案例中,这个变量仅在JSON分配字节空间时起到作用,而不像第一个案例多个地方使用的时候从线程获取该变量

    这里使用ThreadLocal是因为每个线程分配空间可能不一样,因此每个线程都要有自己的字节空间,要有自己的备份

        private final static ThreadLocal<byte[]> bytesLocal = new ThreadLocal<byte[]>();
        private static byte[] allocateBytes(int length) {
            byte[] chars = bytesLocal.get();
    
            if (chars == null) {
                if (length <= 1024 * 64) {
                    chars = new byte[1024 * 64];
                    bytesLocal.set(chars);
                } else {
                    chars = new byte[length];
                }
            } else if (chars.length < length) {
                chars = new byte[length];
            }
    
            return chars;
        }

    (3)cat日志中的tags

    使用cat打日志时需要传一个map参数,用于追踪日志,用法如下,第三个参数是一个map

    logger.info(GlobalContext.getLogTitle(), String.format("调用接口,request:%s", SerializationUtils.serialize(request)), GlobalContext.getLogtags());

    3.1 对于每一个请求,都是由一个线程取处理,每个线程接到的请求是不同的,因此打日志时传入的map也是不一样的,这就符合ThreadLocal的第一种场景 -- 变量值只与当前线程有关,关于该变量每个线程有自己的备份

    3.2 处理一次web请求要多个地方打日志,如果在每个方法参数里都传这个map的话会显得很繁琐,这就符合ThreadLocal的第二种场景 -- 变量在线程执行过程中需要多次使用

    有一种思路是用一个静态公共类去存储与提取map,但这个map在每个请求也就是每个线程的值都是不一样的,因此我们使用ThreadLocal来存储,每个线程都有自己的备份map,通过公共类去存储与获取

    public final class GlobalContext {
        private static final ThreadLocal<Map<String, String>> localLogTags = ThreadLocal.withInitial(HashMap::new);
    
        private GlobalContext() {}
    
        public static Map<String, String> getLogTags() {
            return localLogTags.get();
        }
    
        public static void setLogTags(Map<String, String> logTags) {
            localLogTags.set(logTags);
        }
    
        public static void addLogTags(String tagName, String tagValue) {
            localLogTags.get().put(tagName, tagValue);
        }
    }

    (四)原理

    (1)线程与本地变量的map对应关系由线程来维护,这个Map的key为ThreadLocal,value就是本地变量Object

    查看Thread源码可以看到这个变量

        /* ThreadLocal values pertaining to this thread. This map is maintained
         * by the ThreadLocal class. */
        ThreadLocal.ThreadLocalMap threadLocals = null;

    这样设计不需要额外的加锁来保证Map关系读写

    (2)ThreadLocal.get()

        public T get() {
            Thread t = Thread.currentThread();
            ThreadLocalMap map = getMap(t);
            if (map != null) {
                ThreadLocalMap.Entry e = map.getEntry(this);
                if (e != null) {
                    @SuppressWarnings("unchecked")
                    T result = (T)e.value;
                    return result;
                }
            }
            return setInitialValue();
        }

    2.1 ThreadLocal.getMap(t)就获取到当前线程ThreadLocal与变量的映射关系map

        ThreadLocalMap getMap(Thread t) {
            return t.threadLocals;
        }

    查看Thread的源码,可以看到有这个域,它的访问级别是package

        /* ThreadLocal values pertaining to this thread. This map is maintained
         * by the ThreadLocal class. */
        ThreadLocal.ThreadLocalMap threadLocals = null;

    而由因为ThreadLocal跟Thread在同一个包下,所以getMap()能直接获取到映射关系map

    2.2 把自身this作为key值从map中查找对应的value,也就是我们的局部变量

    2.3 如果map是空, 通过ThreadLocal.setInitialValue()初始化该线程的映射关系map

        private T setInitialValue() {
            T value = initialValue();
            Thread t = Thread.currentThread();
            ThreadLocalMap map = getMap(t);
            if (map != null)
                map.set(this, value);
            else
                createMap(t, value);
            return value;
        }

    观察setInitialValue(),首先通过ThreadLocal.initialValue()来获取该ThreadLocal变量的初始值

    initialValue()默认是返回null的,但是我们可以在创建ThreadLocal变量时重写该方法来定义一个默认返回,例如:

        ThreadLocal<String> day = new ThreadLocal<String>(){
            @Override
            protected String initialValue() {
                return "sunday";
            }
        };

    现在更推荐另一种方法在初始化时赋默认值:

    ThreadLocal<String> day = ThreadLocal.withInitial(() -> "sunday");

     (3)ThreadLocal.set()

        public void set(T value) {
            Thread t = Thread.currentThread();
            ThreadLocalMap map = getMap(t);
            if (map != null)
                map.set(this, value);
            else
                createMap(t, value);
        }

    3.1 ThreadLocal.getMap(t)就获取到当前线程ThreadLocal与变量的映射关系map

    3.2 map不空就把当前ThreadLocal作为key,要保存的值作为value写入线程的映射关系map

    3.3 map为空就调用createMap方法来初始化map

    (五)注意事项

    (1)ThreadLocal变量通常被定义为private static

    {@code ThreadLocal} instances are typically private
     static fields in classes that wish to associate state with a thread(e.g., a user ID or Transaction ID).

    这样做的目的是希望与当前线程的状态相关联,例如对于一个请求,userID在整个线程处理流程中都要用到,每个请求处理线程的userID都可能不一样,因此使用private static ThreadLocal

    (2)使用完ThreadLocal变量后,确定不再需要用到该变量,调用其remove方法

  • 相关阅读:
    zt 必看: 原来PCIe技术原理这么简单!
    zt linux:centos 解决SSH连接Linux超时自动断开
    idea总结和未来的想法
    linux一些技巧
    zt如何解决Verilog目前不支持数组型端口定义!
    高速设计学习-干货!高速串行Serdes均衡之FFE
    zt:tcpdump抓包对性能的影响
    zt:TCP 学习
    verdi使用
    IE 浏览器下 button元素自动触发click?
  • 原文地址:https://www.cnblogs.com/ming-szu/p/11413427.html
Copyright © 2011-2022 走看看