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:\Java\jdk1.8.0_131\bin\java.exe "-javaagent:D:\JetBrains\IntelliJ IDEA Community Edition 2019.1.1\lib\idea_rt.jar=53791:D:\JetBrains\IntelliJ IDEA Community Edition 2019.1.1\bin" -Dfile.encoding=UTF-8 -classpath D:\Java\jdk1.8.0_131\jre\lib\charsets.jar;D:\Java\jdk1.8.0_131\jre\lib\deploy.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;D:\Java\jdk1.8.0_131\jre\lib\javaws.jar;D:\Java\jdk1.8.0_131\jre\lib\jce.jar;D:\Java\jdk1.8.0_131\jre\lib\jfr.jar;D:\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;D:\Java\jdk1.8.0_131\jre\lib\jsse.jar;D:\Java\jdk1.8.0_131\jre\lib\management-agent.jar;D:\Java\jdk1.8.0_131\jre\lib\plugin.jar;D:\Java\jdk1.8.0_131\jre\lib\resources.jar;D:\Java\jdk1.8.0_131\jre\lib\rt.jar;D:\idea_workspace\july-project\java-base\target\classes;D:\repository\org\projectlombok\lombok\1.18.8\lombok-1.18.8.jar;D:\repository\junit\junit\4.12\junit-4.12.jar;D:\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-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使用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章