zoukankan      html  css  js  c++  java
  • ThreadLocal是什么?使用场景有哪些?

    什么是ThreadLocal?

      ThreadLocal为每个使用该变量的线程提供独立的变量副本,所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本。

    测试代码:

    package com.javaBase.LineDistance;
    
    /**
     * 〈一句话功能简述〉;
     * 〈功能详细描述〉
     *
     * @author jxx
     * @see [相关类/方法](可选)
     * @since [产品/模块版本] (可选)
     */
    public class TestThreadLocal {
    
        public static void main(String[] args) {
    
            ThreadLocal<Integer> threadLocal = new MyThreadLocal();
    
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 3; i++) {
                        threadLocal.set(threadLocal.get() + 1);
                        System.out.println("线程1:" + threadLocal.get());
                    }
                }
            });
    
            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 3; i++) {
                        threadLocal.set(threadLocal.get() + 1);
                        System.out.println("线程2:" + threadLocal.get());
                    }
                }
            });
    
            Thread t3 = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 3; i++) {
                        threadLocal.set(threadLocal.get() + 1);
                        System.out.println("线程3:" + threadLocal.get());
                    }
                }
            });
    
            t1.start();
            t2.start();
            t3.start();
        }
    
        private static class MyThreadLocal extends ThreadLocal<Integer> {
    
            @Override
            protected Integer initialValue() {
                return 0;
            }
        }
    
    }

    执行结果:

    线程2:1
    线程1:1
    线程2:2
    线程3:1
    线程1:2
    线程3:2
    线程2:3
    线程3:3
    线程1:3

    有结果可知个线程之间对ThreadLocal的操作互不影响。

    ThreadLocal原理

    ThreadLocal中的几个主要方法:

    • void set(Object value)设置当前线程的线程局部变量的值。
    • public Object get()该方法返回当前线程所对应的线程局部变量。
    • public void remove()将当前线程局部变量的值删除,目的是为了减少内存的占用,该方法是JDK 5.0新增的方法。需要指出的是,当线程结束后,对应该线程的局部变量将自动被垃圾回收,所以显式调用该方法清除线程的局部变量并不是必须的操作,但它可以加快内存回收的速度。
    • protected Object initialValue()返回该线程局部变量的初始值,该方法是一个protected的方法,显然是为了让子类覆盖而设计的。这个方法是一个延迟调用方法,在线程第1次调用get()或set(Object)时才执行,并且仅执行1次。ThreadLocal中的缺省实现直接返回一个null。

    get和set方法源码:

    /**
         * Returns the value in the current thread's copy of this
         * thread-local variable.  If the variable has no value for the
         * current thread, it is first initialized to the value returned
         * by an invocation of the {@link #initialValue} method.
         *
         * @return the current thread's value of this thread-local
         */
        public T get() {
            Thread t = Thread.currentThread();
            ThreadLocalMap map = getMap(t);
            if (map != null) {
                ThreadLocalMap.Entry e = map.getEntry(this);
                if (e != null) {
                    @SuppressWarnings("unchecked")
                    T result = (T)e.value;
                    return result;
                }
            }
            return setInitialValue();
        }
    
    /**
         * Sets the current thread's copy of this thread-local variable
         * to the specified value.  Most subclasses will have no need to
         * override this method, relying solely on the {@link #initialValue}
         * method to set the values of thread-locals.
         *
         * @param value the value to be stored in the current thread's copy of
         *        this thread-local.
         */
        public void set(T value) {
            Thread t = Thread.currentThread();
            ThreadLocalMap map = getMap(t);
            if (map != null)
                map.set(this, value);
            else
                createMap(t, value);
        }

      线程隔离的秘密,就在于ThreadLocalMap这个类。ThreadLocalMap是ThreadLocal类的一个静态内部类,它实现了键值对的设置和获取(对比Map对象来理解),每个线程中都有一个独立的ThreadLocalMap副本,它所存储的值,只能被当前线程读取和修改。ThreadLocal类通过操作每一个线程特有的ThreadLocalMap副本,从而实现了变量访问在不同线程中的隔离。因为每个线程的变量都是自己特有的,完全不会有并发错误。还有一点就是,ThreadLocalMap存储的键值对中的键是this对象指向的ThreadLocal对象,而值就是你所设置的对象了。

    ThreadLocal的应用场景

    1、方便同一个线程使用某一对象,避免不必要的参数传递;
    2、线程间数据隔离(每个线程在自己线程里使用自己的局部变量,各线程间的ThreadLocal对象互不影响);
    3、获取数据库连接、Session、关联ID(比如日志的uniqueID,方便串起多个日志);
    其中spring中的事务管理器就是使用的ThreadLocal:
      Spring的事务管理器通过AOP切入业务代码,在进入业务代码前,会依据相应的事务管理器提取出相应的事务对象,假如事务管理器是DataSourceTransactionManager,
    就会从DataSource中获取一个连接对象,通过一定的包装后将其保存在ThreadLocal中。而且Spring也将DataSource进行了包装,重写了当中的getConnection()方法,或者说
    该方法的返回将由Spring来控制,这样Spring就能让线程内多次获取到的Connection对象是同一个。

    参考链接:彻底理解ThreadLocal

           java ThreadLocal(应用场景及使用方式及原理)

  • 相关阅读:
    @从零开始实现一个插件化框架(一)
    @从零开始实现一个插件化框架(二)
    @从零开始实现一个插件化框架(三)
    @CoordinatorLayout使用详解: 打造折叠悬浮效果
    Oracle 11g数据库详细安装过程
    web service 的跨语言特性
    struts2--值栈
    事务处理中如何获取同一个connection 对象
    jsp中文乱码问题
    设置工作集
  • 原文地址:https://www.cnblogs.com/jxxblogs/p/11898870.html
Copyright © 2011-2022 走看看