• ThreadLocal


    ThreadLocal字面意思本地线程,ThreadLocal使每个线程之间是隔离的,数据是独立的,我们使用过session都知道 session是一个会话,我们可以用它来存储一些用户的基本信息,这样每个用户在服务端都能取到,ThreadLocal也可以做到, ThreadLocal将相应的信息存储在当前的线程中,只有当前线程能够访问,其他线程不能访问,其实ThreadLocal 可以说是一个定制化的Map。

    下面我们通过一个示例来演示怎么使用ThreadLocal,我们用ThreadLocal来存储用户对象,然后取出用户对象,移除 用户对象 threadLocal.set(threadLocalUser)存储用户的信息 threadLocal.get()获取当前线程存储的信息 threadLocal.remove()移除当前线程储存的信息

    如果我们在executorService.shutdown()关闭线程池后再去get(),那么将会返回null,因为线程池已经关闭 不知道是从那个线程中取,所以返回null。

    package thread;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    /**
     * ThreadLocal
     */
    public class ThreadLocalTest {
    
        private static ThreadLocal<ThreadLocalUser> threadLocal = new ThreadLocal<ThreadLocalUser>();
    
        final static ExecutorService executorService = Executors.newCachedThreadPool();
    
        /**
         * 获取本地线程值
         */
        static void getThreadLocalUser(){
            System.out.println(Thread.currentThread().getName()+"  :  "+threadLocal.get());
        }
    
        /**
         * 移除本地线程值
         */
        static void removeThreadLocalUser(){
            threadLocal.remove();
            System.out.println(Thread.currentThread().getName()+"  :  "+threadLocal.get());
        }
    
        public static void main(String[] args) {
            executorService.submit(() ->{
                ThreadLocalUser threadLocalUser = new ThreadLocalUser();
                threadLocalUser.setUserId("123456");
                threadLocalUser.setRoleId("1");
                threadLocal.set(threadLocalUser);
                getThreadLocalUser();
                removeThreadLocalUser();
            });
    
            executorService.submit(() ->{
                ThreadLocalUser threadLocalUser = new ThreadLocalUser();
                threadLocalUser.setUserId("131420");
                threadLocalUser.setRoleId("2");
                threadLocal.set(threadLocalUser);
                getThreadLocalUser();
                removeThreadLocalUser();
            });
    
            executorService.shutdown();
        }
    }

    我们可以看出,ThreadLocal其实是存放在threadLocals,而threadLocals是ThreadLocalMap 的一个成员变量,所以ThreadLocal实际上只是一个外壳,里面层层封装

      public void set(T value) {
            Thread t = Thread.currentThread();
            ThreadLocalMap map = getMap(t);
                if (map != null)
            map.set(this, value);
                else
            createMap(t, value);
        }
        
        ThreadLocal.ThreadLocalMap threadLocals = null;
        
        ThreadLocalMap getMap(Thread t) {
            return t.threadLocals;
        }
    生命不止,折腾不息
  • 相关阅读:
    Jenkins持续集成邮件发送
    基于appium快速实现H5自动化测试
    Linux常用命令学习一
    BZOJ4372烁烁的游戏——动态点分治+线段树(点分树套线段树)
    BZOJ3730震波——动态点分治+线段树(点分树套线段树)
    BZOJ1014[JSOI2008]火星人——非旋转treap+二分答案+hash
    BZOJ1299[LLH邀请赛]巧克力棒——Nim游戏+搜索
    BZOJ1115[POI2009]石子游戏——阶梯Nim游戏
    BZOJ3110[Zjoi2013]K大数查询——权值线段树套线段树
    BZOJ5343[Ctsc2018]混合果汁——主席树+二分答案
  • 原文地址:https://www.cnblogs.com/steakliu/p/15229284.html
Copyright © 2020-2023  润新知