• 每天一个面试题:ThreadLocal底层原理和实现Demo


    今天开始一个新专栏:每天一个面试题系列
    看来自己学习方式和能力还是差很多,痛定思痛,改变自己的学习方式,辞去原有的实习,
    开始全新的学习,沉淀才会有产出,一步一脚印!
    面试题系列搞起来,这个专栏并非单纯的八股文,我会在技术底层的基础上,不至于Debug,还会做一些实例的实现,实现一些简单的Demo,或者用于我做过的项目中去;
    代码会同步在我的gitee中去,觉得不错的同学记得一键三连求关注,感谢:
    链接: ThreadLocal

    每天一个面试题:ThreadLocal

    在这里插入图片描述

    1. ThreadLocal表示线程的“局部变量”,它确保每个线程的ThreadLocal变量都是各自独立的;

    2. ThreadLocal适合在一个线程的处理流程中保持上下文(避免了同一参数在所有方法中传递);

    3. 使用ThreadLocal要用try … finally结构,并在finally中清除。

    实现ThreadLocal的Demo

    多个线程,我们希望每个线程内只连接到自己的数据库

    public class DemoThreadLocal {
    
    
        public static void main(String[] args) {
            test();
        }
    
        private static void test() {
    
    
            for (int i = 0; i < 5; i++) {
                new Thread(()->{
    //多个线程,我们希望每个线程内只连接到自己的数据库,这里的打印相当 各个用户 操作自己的数据库
                    System.out.println(Utils.getConnection().toString());
                    System.out.println(Utils.getConnection().toString());
                    System.out.println(Utils.getConnection().toString());
                }, "i" + (i+1)).start();
            }
        }
    
        static class Utils{
    	
            private static final  ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
    		
    		// 通过本地线程管理各自的数据库连接
            public static Connection getConnection(){
                Connection con = threadLocal.get();
                if (con==null) {
                    con = getIn();
                    threadLocal.set(con);
                }
                return con;
            }
    
    		//配置数据库连接
            public static Connection getIn(){
    
                try {
                    return DriverManager.getConnection("jdbc:mysql://localhost:3300/blog?useSSL=false","root","root");
    
                }catch (Exception e){
                    throw new RuntimeException(e);
                }
            }
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    在这里插入图片描述

    ThreadLocal底层原理

    在这里插入图片描述

    在这里插入图片描述

    通过hash计算去计算资源对应的位置索引,进行存放

    在这里插入图片描述

    如果出现了Hash冲突:拉链法、其他算法

    在这里插入图片描述

    为什么ThreadLocalMap的key设计为弱引用

    在这里插入图片描述
    在这里插入图片描述

    强引用无法被GC,所以使用ThreadLocal要用try … finally结构,并在finally中清除。

    在这里插入图片描述

    ThreadMap在get时,如果没有值,也会执行set (key,null)操作

    在这里插入图片描述

    三大情况

    • 发现key= null时

    在这里插入图片描述

    • GC自动收回在这里插入图片描述
    • 手动remove

    在这里插入图片描述

  • 相关阅读:
    用ChatGPT编写一个词卡显示网页
    【数据结构】Set和Map
    【Golang开发面经】知乎(两轮技术面)
    UNDO表空间使用率过高的处理方式
    ArrayList 可以完全替代数组吗?
    【数据结构】广义表与二叉树之间的转换
    ApplicationContext种类
    TypeError: %c requires int or char
    视频压缩软件哪个好?万兴优转:好用的视频无损压缩软件
    KBPC5010W-ASEMI金属壳针脚方桥KBPC5010W
  • 原文地址:https://blog.csdn.net/futurn_hero/article/details/128193181