ThreadLocal原理和使用场景

21 篇文章 0 订阅
5 篇文章 0 订阅
 
 
ThreadLocal与线程成员变量还有区别,ThreadLocal该类提供了线程局部变量。这个局部变量与一般的成员变量不一样,ThreadLocal的变量在被多个线程使用时候,每个线程只能拿到该变量的一个副本,这是Java API中的描述,通过阅读API源码,发现并非副本,副本什么概念?克隆品? 或者是别的样子,太模糊。
 
准确的说,应该是ThreadLocal类型的变量内部的注册表(Map<Thread,T>)发生了变化,但ThreadLocal类型的变量本身的确是一个,这才是本质!
 
下面就做个例子:
 
一、标准例子
 
定义了MyThreadLocal类,创建它的一个对象tlt,分别给四个线程使用,结果四个线程tlt变量并没有出现共用现象,二是各用各的,这说明,四个线程使用的是tlt的副本(克隆品)。
 
/**
* 使用了ThreadLocal的类
*
* @author leizhimin 2010-1-5 10:35:27
*/
public class MyThreadLocal {

    private static MyThreadLocal myThreadLocal = null;
    private  ThreadLocal<Integer> td=new ThreadLocal <>();
    public static MyThreadLocal getInstance(){
        if(myThreadLocal==null){
            myThreadLocal = new MyThreadLocal();
            return myThreadLocal;
        }else{
            return myThreadLocal;
        }
    }
    public MyThreadLocal(ThreadLocal<Integer> td){
        this.td = td;
    }

    public MyThreadLocal() {

    }
    protected void setInit(Integer n) {
        td.set(n);
    }
    public Integer getnextNum(){
        td.set(td.get()+1);
        return td.get();
    }



}
 
/**
* 测试线程
*
* @author leizhimin 2010-1-5 10:39:18
*/

public class TestThread extends Thread {
       
private MyThreadLocal tlt = MyThreadLocal.getInstance();
        public TestThread(MyThreadLocal tlt) {
                this.tlt = tlt;
        }

        @Override
        public void run() {
                for (int i = 0; i < 3; i++) {
                        System.out.println(Thread.currentThread().getName() + "\t" + tlt.getNextNum());
                }
        }
}
 
/**
* ThreadLocal测试
*
* @author leizhimin 2010-1-5 10:43:48
*/

public class Test {
        public static void main(String[] args) {
                MyThreadLocal tlt = new MyThreadLocal();
                Thread t1 = new TestThread(tlt);
                Thread t2 = new TestThread(tlt);
                Thread t3 = new TestThread(tlt);
                Thread t4 = new TestThread(tlt);
                t1.start();
                t2.start();
                t3.start();
                t4.start();

        }
}
 
可以看出,三个线程各自独立编号,互不影响:
Thread-0  1
Thread-1  1
Thread-0  2
Thread-1  2
Thread-0  3
Thread-1  3
Thread-2  1
Thread-3  1
Thread-2  2
Thread-3  2
Thread-2  3
Thread-3  3

Process finished with exit code 0
 
tlt对象是一个,废话tl对象也是一个,因为组合关系是一对一的。但是tl对象内部的Map随着线程的增多,会创建很多Integer对象。只是Integer和int已经通用了。所以感觉不到Integer的对象属性。
 
二、不用ThreadLocal
 
假如不用ThreadLocal,只需要将MyThreadLocal类重新定义为:
/**
* 使用了ThreadLocal的类
*
* @author leizhimin 2010-1-5 10:35:27
*/

public class MyThreadLocal {
        private Integer t1 = 0;
        public Integer getNextNum(){
                return t1=t1+1;
        }



//        //定义了一个ThreadLocal变量,用来保存int或Integer数据
//        private ThreadLocal<Integer> tl = new ThreadLocal<Integer>() {
//                @Override
//                protected Integer initialValue() {
//                        return 0;
//                }
//        };
//
//        public Integer getNextNum() {
//                //将tl的值获取后加1,并更新设置t1的值
//                tl.set(tl.get() + 1);
//                return tl.get();
//        }
}
 
然后运行测试:
Thread-2  1
Thread-2  2
Thread-1  4
Thread-1  6
Thread-3  3
Thread-3  9
Thread-3  10
Thread-1  8
Thread-0  7
Thread-0  11
Thread-0  12
Thread-2  5

Process finished with exit code 0
 
从这里可以看出,四个线程共享了tlt变量,结果每个线程都直接修改tlt的属性。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wx: fulltilt8

文章原创,对你有用的话请打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值