Thread详解12:InheritableThreadLocal的使用

ThreadLocal的用法可以参考上一篇博文: http://blog.csdn.net/cds86333774/article/details/51020819,InheritableThreadLocal是ThreadLocal的子类。该类扩展了 ThreadLocal,为子线程提供从父线程那里继承的值:在创建子线程时,子线程会接收所有可继承的线程局部变量的初始值,以获得父线程所具有的值。通常,子线程的值与父线程的值是一致的;但是,通过重写这个类中的 childValue 方法,子线程的值可以作为父线程值的一个任意函数。当必须将变量(如用户 ID 和 事务 ID)中维护的每线程属性(per-thread-attribute)自动传送给创建的所有子线程时,应尽可能地采用可继承的线程局部变量,而不是采用普通的线程局部变量。

1 子线程

如果线程A创建了线程B,那么B就是A的子线程。


2 示例代码

用法和ThreadLocal几乎一样,但是效果不一样。

ThreadLocalTest.java

package threadLocalTest;

import java.util.Random;

public class ThreadLocalTest {
    private static ThreadLocal<Integer> familyFortunes = new ThreadLocal<Integer>() {
        @Override
        protected Integer initialValue() {
            Random random = new Random();
            return random.nextInt(1000);
        }
    };

    public static int get() {
        return familyFortunes.get();
    }

    public static void set(int value) {
        familyFortunes.set(value);
    }

}

InheritableThreadLocalTest.java

package threadLocalTest;

import java.util.Random;

public class InheritableThreadLocalTest {
    private static InheritableThreadLocal<Integer> familyFortunes = new InheritableThreadLocal<Integer>() {
        @Override
        protected Integer initialValue() {
            Random random = new Random();
            return random.nextInt(1000);
        }
    };

    public static int get() {
        return familyFortunes.get();
    }

    public static void set(int value) {
        familyFortunes.set(value);
    }

}

Thread2.java

package threadLocalTest;

public class Thread2 extends Thread {

    public Thread2(String name) {
        super(name);
    }

    @Override
    public void run() {
        super.run();
        System.out.printf("%s 从 ThreadLocal 取数据:%d\n", Thread.currentThread().getName(), ThreadLocalTest.get());
        System.out.printf("%s 从 InheritableThreadLocal 取数据:%d\n", Thread.currentThread().getName(),
                InheritableThreadLocalTest.get());
    }

    public static void main(String[] args) {
        System.out.printf("%s 从 ThreadLocal 取数据:%d\n", Thread.currentThread().getName(), ThreadLocalTest.get());
        System.out.printf("%s 从 InheritableThreadLocal 取数据:%d\n", Thread.currentThread().getName(),
                InheritableThreadLocalTest.get());
        Thread2 t1 = new Thread2("Child1");
        t1.start();
    }
}

输出

main 从 ThreadLocal 取数据:444
main 从 InheritableThreadLocal 取数据:401
Child1 从 ThreadLocal 取数据:513
Child1 从 InheritableThreadLocal 取数据:401

分析

  1. 不同的线程去 ThreadLocal get 数据,得到的是和自己绑定的数据。
  2. 如果是子线程去 InheritableThreadLocal get 数据,得到的是和父线程一样的数据,也就是子线程继承了父线程在 InheritableThreadLocal 里面的数据,这个数据变成了“家族数据”

3 childValue

看完childValue方法的说明,我还是不知道它怎么用,我们来看看源码:

    /**
     * Computes the child's initial value for this inheritable thread-local
     * variable as a function of the parent's value at the time the child
     * thread is created.  This method is called from within the parent
     * thread before the child is started.
     * <p>
     * This method merely returns its input argument, and should be overridden
     * if a different behavior is desired.
     *
     * @param parentValue the parent thread's value
     * @return the child thread's initial value
     */
    protected T childValue(T parentValue) {
        return parentValue;
    }

原来这个方法返回的是子线程在第一次get的时候的初始值,如果不重写,默认就是返回父线程的值,所以出现上面示例代码的输出。在形式上和ThreadLocal的initialValue类似。

下面我重写childValue,使子线程的初始值为父线程的值+1:

package threadLocalTest;

import java.util.Random;

public class InheritableThreadLocalTest {
    private static InheritableThreadLocal<Integer> familyFortunes = new InheritableThreadLocal<Integer>() {
        @Override
        protected Integer initialValue() {
            Random random = new Random();
            return random.nextInt(1000);
        }

        @Override
        protected Integer childValue(Integer parentValue) {
            return parentValue+1;
        }


    };

    public static int get() {
        return familyFortunes.get();
    }

    public static void set(int value) {
        familyFortunes.set(value);
    }



}

输出

main 从 ThreadLocal 取数据:506
main 从 InheritableThreadLocal 取数据:570
Child1 从 ThreadLocal 取数据:953
Child1 从 InheritableThreadLocal 取数据:571
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值