zoukankan      html  css  js  c++  java
  • ThreadLocal学习理解

    ThreadLocal作用:
    1.与当前线程绑定,存储当前线程的变量副本,只要是该线程未结束,都可以通过get方法获取到变量;各线程有互相隔离;
    2.可以达到线程安全的效果,避免同步带来的性能消耗。

    简单的小例子:

    /**
     *
     * ThreadLocal使用案例一
     *    模拟线程数据隔离
     * @author Kevin
     */
    public class ThreadLocalDemo1 {
    
        private static ThreadLocal<Integer> threadLocal = new ThreadLocal<>();
    
        public static void main(String[] args) {
    
            Thread t1 = new Thread(new MyRequest(true));
            t1.start();
            Thread t2 = new Thread(new MyRequest(true));
            t2.start();
    
            Thread t3 = new Thread(new MyRequest(false));
            t3.start();
    
    
        }
    
        static class MyRequest implements Runnable {
    
            private boolean setThreadLocal;
    
            MyRequest(boolean setThreadLocal){
                this.setThreadLocal = setThreadLocal;
            }
            @Override
            public void run(){
                try {
                    for(int i=0;i<5;i++){
                        if(setThreadLocal) {
                            threadLocal.set(i);
                        }
                        System.out.println("this current thread name is :" + Thread.currentThread().getName() + "[" +  threadLocal.get() + "]");
                    }
                }finally {
                    /**
                     * 这里把ThreadLocal定义为static还有一个好处就是,由于ThreadLocal有强引用在,
                     * 那么在ThreadLocalMap里对应的Entry的键会永远存在,那么执行remove的时候就可以正确进行定位到并且删除!!!
                     */
                    threadLocal.remove();
                }
            }
        }
    }
    

    结果:

    D:Javajdk1.8.0_131injava.exe "-javaagent:D:JetBrainsIntelliJ IDEA Community Edition 2019.1.1libidea_rt.jar=53791:D:JetBrainsIntelliJ IDEA Community Edition 2019.1.1in" -Dfile.encoding=UTF-8 -classpath D:Javajdk1.8.0_131jrelibcharsets.jar;D:Javajdk1.8.0_131jrelibdeploy.jar;D:Javajdk1.8.0_131jrelibextaccess-bridge-64.jar;D:Javajdk1.8.0_131jrelibextcldrdata.jar;D:Javajdk1.8.0_131jrelibextdnsns.jar;D:Javajdk1.8.0_131jrelibextjaccess.jar;D:Javajdk1.8.0_131jrelibextjfxrt.jar;D:Javajdk1.8.0_131jrelibextlocaledata.jar;D:Javajdk1.8.0_131jrelibext
    ashorn.jar;D:Javajdk1.8.0_131jrelibextsunec.jar;D:Javajdk1.8.0_131jrelibextsunjce_provider.jar;D:Javajdk1.8.0_131jrelibextsunmscapi.jar;D:Javajdk1.8.0_131jrelibextsunpkcs11.jar;D:Javajdk1.8.0_131jrelibextzipfs.jar;D:Javajdk1.8.0_131jrelibjavaws.jar;D:Javajdk1.8.0_131jrelibjce.jar;D:Javajdk1.8.0_131jrelibjfr.jar;D:Javajdk1.8.0_131jrelibjfxswt.jar;D:Javajdk1.8.0_131jrelibjsse.jar;D:Javajdk1.8.0_131jrelibmanagement-agent.jar;D:Javajdk1.8.0_131jrelibplugin.jar;D:Javajdk1.8.0_131jrelib
    esources.jar;D:Javajdk1.8.0_131jrelib
    t.jar;D:idea_workspacejuly-projectjava-base	argetclasses;D:
    epositoryorgprojectlomboklombok1.18.8lombok-1.18.8.jar;D:
    epositoryjunitjunit4.12junit-4.12.jar;D:
    epositoryorghamcresthamcrest-core1.3hamcrest-core-1.3.jar com.base.thread.ThreadLocalDemo1
    this current thread name is :Thread-1[0]
    this current thread name is :Thread-2[null]
    this current thread name is :Thread-2[null]
    this current thread name is :Thread-2[null]
    this current thread name is :Thread-2[null]
    this current thread name is :Thread-0[0]
    this current thread name is :Thread-2[null]
    this current thread name is :Thread-1[1]
    this current thread name is :Thread-0[1]
    this current thread name is :Thread-0[2]
    this current thread name is :Thread-1[2]
    this current thread name is :Thread-0[3]
    this current thread name is :Thread-0[4]
    this current thread name is :Thread-1[3]
    this current thread name is :Thread-1[4]
    
    Process finished with exit code 0
    

    待补充完善:

    • 线程池下的ThreadLocal使用思考,remove的重要性
    • 深层剖析Thread ThreadLocalMap ThreadLocal关系
    • ThreadLocal get和set方法
    • 应用案例分析

    参考:
    Java ThreadLocal的使用
    手撕面试题threadlocal
    threadlocal使用

  • 相关阅读:
    Title
    2019 年 Java 最新面试指南共 80 题,赶快收藏起来吧!
    1+x证书《Web前端开发》等级考试样题
    云服务器、VPS、虚拟主机三者之间的区别?
    1+X”中级Web前端证书对应课程分析
    轻松装Win10:VMware Workstation 12虚拟机下载
    网站收录提交入口
    使用coding和hexo快速搭建博客
    宝塔安装Lsky Pro图床教程
    jsDeliver+github使用教程,免费的cdn
  • 原文地址:https://www.cnblogs.com/Kevin-1992/p/12608348.html
Copyright © 2011-2022 走看看