Java之ThreadLocal

ThreadLocal:

ThreadLocal是一个关于创建线程局部变量的类,可以有效的保证线程之间的数据不会被覆盖,可以进行线程的数据隔离。
1. public T get():可以获取 ThreadLocal在当前线程中保存的变量副本。
源码:
public T get() {
        Thread t = Thread.currentThread();
        ThreadLocal.ThreadLocalMap map = this.getMap(t);
        if (map != null) {
            ThreadLocal.ThreadLocalMap.Entry e = map.getEntry(this);
            if (e != null) {
                T result = e.value;
                return result;
            }
        }

        return this.setInitialValue();	//setInitialValue为初始化对象,set没有赋值默认调用
    }
2. public void set(T value):设置当前线程中变量的副本。
源码:
public void set(T value) {
        Thread t = Thread.currentThread();
        ThreadLocal.ThreadLocalMap map = this.getMap(t);
        if (map != null) {
            map.set(this, value);
        } else {
            this.createMap(t, value);
        }

    }
3. public void remove():删除线程中变量的副本。
源码:
  public void remove() {
        ThreadLocal.ThreadLocalMap m = this.getMap(Thread.currentThread());
        if (m != null) {
            m.remove(this);
        }

    }
4. protected T initialValue():主要用于初始化,是一个protected的方法,主要用于让子类覆写,在线程第1次调用 get() 或 set() 时执行,只执行1次,返回的值为null。
源码:
protected T initialValue() {
        return null;
    }
方法实际的应用:
class Test {
    private static final ThreadLocal<String> local = new ThreadLocal<>();

    public void test() {
        local.set("Hello World");   		//设置值
        System.out.println(local.get());    //获取值
        local.remove();     				//删除值
        System.out.println(local.get());    //获取值
    }
}
public class Demo02 {
    public static void main(String[] args) {
        Test test = new Test();
        test.test();
    }
}
结果:
Hello World
null

案例:当不使用ThreadLocal的情况下,多个线程的数据会被覆盖掉:
class FriendA{
    private String info;

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }
}

class Intermediate{
    public static FriendA friendA;		//定义FriendA对象
}
class FriendB{
    public void print(){
        FriendA friendA = Intermediate.friendA;		//获取传递过来的值
        System.out.println(friendA.getInfo());
    }
}

public class TestThreadLocal {
    public static void main(String[] args) {
        new Thread(()->{
            FriendA friendA = new FriendA();
            friendA.setInfo("123木头人!!!");
            Intermediate.friendA = friendA;		//将friendA赋给Intermediate.friendA 
            FriendB friendB = new FriendB();
            friendB.print();
        }).start();
        new Thread(()->{
            FriendA friendA = new FriendA();
            friendA.setInfo("456木头人!!!");
            Intermediate.friendA = friendA;		//将friendA赋给Intermediate.friendA 
            FriendB friendB = new FriendB();
            friendB.print();
        }).start();
        new Thread(()->{
            FriendA friendA = new FriendA();
            friendA.setInfo("789木头人!!!");
            Intermediate.friendA = friendA;		//将friendA赋给Intermediate.friendA 
            FriendB friendB = new FriendB();
            friendB.print();
        }).start();
    }
}
结果:
456木头人!!!
456木头人!!!
456木头人!!!

案例:当使用ThreadLocal时,可以解决多个线程的覆盖问题:
class FriendA{
    private String info;

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }
}

class Intermediate{
	//私有化让其他的类访问不到并且使用final不可以改变
    private static final ThreadLocal<FriendA> local = new ThreadLocal();	//定义ThreadLocal类实例,指定FriendA的泛型类型

    public static void setFriendA(FriendA friendA) {
        local.set(friendA);	//利用ThreadLocal的set()方法输入friendA对象
    }
    public static FriendA getFriendA() {
        return local.get();	//利用ThreadLocal的get()返回friendA对象
    }
}
class FriendB{
    public void print(){
        FriendA friendA = Intermediate.getFriendA();	
        System.out.println(friendA.getInfo());
    }
}

public class TestThreadLocal {
    public static void main(String[] args) {
        new Thread(()->{
            FriendA friendA = new FriendA();
            Intermediate.setFriendA(friendA);
            friendA.setInfo("123木头人!!!");	//输出数据
            FriendB friendB = new FriendB();
            friendB.print();
        }).start();
        new Thread(()->{
            FriendA friendA = new FriendA();
            Intermediate.setFriendA(friendA);	//输出数据
            friendA.setInfo("456木头人!!!");
            FriendB friendB = new FriendB();
            friendB.print();
        }).start();
        new Thread(()->{
            FriendA friendA = new FriendA();
            Intermediate.setFriendA(friendA);	//输出数据
            friendA.setInfo("789木头人!!!");
            FriendB friendB = new FriendB();
            friendB.print();
        }).start();
    }
}
结果:
456木头人!!!
789木头人!!!
123木头人!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值