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> getLocalLogTags() {
        return localLogTags.get();
    }

    public static void setLocalLogTags(Map<String, String> logTags) {
        localLogTags.set(logTags);
    }

    public static void addLogTags(String tagName, String tagValue) {
        localLogTags.get().put(tagName, tagValue);
    }
}

(四)原理

 

(五)注意事项

猜你喜欢

转载自www.cnblogs.com/ming-szu/p/11413427.html
今日推荐