• ThreadLocal的正确使用与原理


    ThreadLocal是什么

    ThreadLocal是线程Thread中属性threadLocals即ThreadLocal.ThreadLocalMap的管理者,ThreadLocal用于给每个线程操作自己线程的本地变量,通过线程私有从而保证线程安全性。

    ThreadLocal原理

    get()方法来说,线程的本地变量是存放在线程实例的属性ThreadLocalMap上的,ThreadLocalMap本质上就是一个HashMap,ThreadLocal只是一个管理者,当我们的线程需要拿到自己的本地变量时,我们直接调用ThreadLocal去get本地变量即可。
    因为get()方法底层会先获取到当前线程,然后通过当前线程拿到他的属性值ThreadLocalMap,如果ThreadLocalMap为空,则会调用ThreadLocal的初始化方法拿到初始值返回,如果不为空,则会拿该ThreadLocal作为key去获取该线程下的ThreadLocalMap里对应的value值。

    ThreadLocal内存泄漏问题

    线程的属性值ThreadLocalMap中使用的 key 为 ThreadLocal 的弱引用,而value是强引用。所以,如果ThreadLocal没有被外部强引用的情况下,在垃圾回收的时候,key 会被清理掉,而value 不会被清理掉。这样的话,ThreadLocalMap 中就会出现 key 为 null 的 Entry。假如我们不做任何措施的话,value 永远无法被 GC 回收,这个时候就可能会产生内存泄露。
    因此针对这种情况,我们有两种原则:

    1. ThreadLocal申明为private static final。JDK建议ThreadLocal定义为private static,这样ThreadLocal的弱引用问题则不存在了。
      • private与final 尽可能不让他人修改变更引用。
      • static 表示为类属性,只有在程序结束才会被回收。
    2. ThreadLocal使用后务必调用remove方法。
      • 最简单有效的方法是使用后将其移除。

    关于InheritableThreadLocal

    InheritableThreadLocal类是ThreadLocal类的子类。ThreadLocal中每个线程拥有它自己的值,与ThreadLocal不同的是,InheritableThreadLocal允许一个线程以及该线程创建的所有子线程都可以访问它保存的值

    代码示例

    ThreadLocal使用

    public class ThreadLocalTest {
    
        //第一种初始化方式
        /**
         * 声明为static是让ThreadLocal实例随着程序的结束才结束,这样才不会让GC回收了
         * 声明为final是让ThreadLocal实例引用不会被替换,这样子也不会因为被替换导致被GC回收
         * 这两个声明都是为了避免作为key的ThreadLocal对象没有外部强引用而导致被GC回收,从而导致内存泄漏的问题,因为ThreadLocalMap<ThreadLocal, Object>中的ThreadLocal
         * 对象作为key是弱引用,会被GC回收。
         */
        private static final ThreadLocal<String> threadLocalStr = ThreadLocal.withInitial(() -> "fresh");
    
        private static AtomicInteger intGen = new AtomicInteger(0);
        //第二种初始化方式
        private static final ThreadLocal<Integer> threadLocalInt = new ThreadLocal<Integer>() {
            @Override
            public Integer initialValue() {
                return intGen.incrementAndGet();
            }
        };
    
        public static void main(String[] args) throws InterruptedException {
            ArrayList<Thread> threads = new ArrayList<>();
            for (int i = 0; i < 2; i++) {
                Thread t = new Thread(() -> {
                    try {
                        System.out.println(Thread.currentThread().getName() + "   " + threadLocalInt.get());
                        System.out.println(Thread.currentThread().getName() + "   " + threadLocalStr.get());
                        TimeUnit.SECONDS.sleep(5);
                        threadLocalStr.set("bojack horseman" + threadLocalInt.get());
                        System.out.println(Thread.currentThread().getName() + "   " + threadLocalInt.get());
                        System.out.println(Thread.currentThread().getName() + "   " + threadLocalStr.get());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        threadLocalInt.remove();
                        threadLocalStr.remove();
                    }
                });
                t.start();
                threads.add(t);
            }
            TimeUnit.SECONDS.sleep(2);
            System.out.println(threads);
            System.out.println(threadLocalStr);
            System.out.println(threadLocalInt);
        }
        /**
         * Thread-0   1
         * Thread-1   2
         * Thread-0   fresh
         * Thread-1   fresh
         * [Thread[Thread-0,5,main], Thread[Thread-1,5,main]]
         * java.lang.ThreadLocal$SuppliedThreadLocal@1ef7fe8e
         * cn.vv.schedule.test.ThreadLocalTest$1@6f79caec
         * Thread-1   2
         * Thread-1   bojack horseman2
         * Thread-0   1
         * Thread-0   bojack horseman1
         */
    
    }
    

    InheritableThreadLocal使用

    public class InheritableThreadLocalTest {
    
        //第一种初始化方式
        private static final InheritableThreadLocal<String> threadLocalStr = new InheritableThreadLocal<String>() {
            @Override
            public String initialValue() {
                return "fresh";
            }
        };
        private static AtomicInteger intGen = new AtomicInteger(0);
        //第二种初始化方式
        private static final ThreadLocal<Integer> threadLocalInt = new ThreadLocal<Integer>() {
            @Override
            public Integer initialValue() {
                return intGen.incrementAndGet();
            }
        };
    
        public static void main(String[] args) throws InterruptedException {
            //如果是InheritableThreadLocal,则父线程创建的所有子线程都会复制一份父线程的线程变量,而不是去初始化一份线程变量
            threadLocalStr.set("main");
            ArrayList<Thread> threads = new ArrayList<>();
            for (int i = 0; i < 2; i++) {
                Thread t = new Thread(() -> {
                    try {
                        System.out.println(Thread.currentThread().getName() + "   " + threadLocalInt.get());
                        System.out.println(Thread.currentThread().getName() + "   " + threadLocalStr.get());
                        TimeUnit.SECONDS.sleep(5);
                        //子线程可以自由地改变自己的本地变量
                        threadLocalStr.set("bojack horseman" + threadLocalInt.get());
                        System.out.println(Thread.currentThread().getName() + "   " + threadLocalInt.get());
                        System.out.println(Thread.currentThread().getName() + "   " + threadLocalStr.get());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        threadLocalInt.remove();
                        threadLocalStr.remove();
                    }
                });
                t.start();
                threads.add(t);
            }
            TimeUnit.SECONDS.sleep(2);
            System.out.println(Thread.currentThread().getName() + "   " + threadLocalStr.get());
        }
        /**
         * Thread-0   2
         * Thread-1   1
         * Thread-0   main
         * Thread-1   main
         * main   main
         * Thread-0   2
         * Thread-0   bojack horseman2
         * Thread-1   1
         * Thread-1   bojack horseman1
         */
    
    }
    

    参考

    ThreadLocal理解及应用

  • 相关阅读:
    codeforces 616B Dinner with Emma
    codeforces 616A Comparing Two Long Integers
    codeforces 615C Running Track
    codeforces 612C Replace To Make Regular Bracket Sequence
    codeforces 612B HDD is Outdated Technology
    重写父类中的成员属性
    子类继承父类
    访问修饰符
    方法的参数
    实例化类
  • 原文地址:https://www.cnblogs.com/castamere/p/15727414.html
Copyright © 2020-2023  润新知