threadLocal 原理与使用

目标:用于每个线程资源的隔离.

当工作于多线程中的对象使用ThreadLocal 维护变量时,ThreadLocal 为 每个使用该变量的线程分配一个独立的变量副本。每个线程独立改变自己的副本,而不影响其他线程所对应的变量副本。

不同于线程同步:线程同步用于解决一个变量同一时刻被多个线程共享的问题(共享一个变量)。
threadLocal 使得一个变量在多个线程中各自拥有自己的副本(实现资源的隔离)。

原理:

ThreadLocal 中有一个map, 用于存储每一个线程的变量副本,其中map的key为 线程对象,value为对应线程的变量副本。

public class SimpleThreadLocal<T> {
    private Map valueMap = Collections.synchronizedMap(new HashMap());

    public void set(T value) {
        valueMap.put(Thread.currentThread(), value);
    }

    public T get() {
        Thread currentThread = Thread.currentThread();
        T value = (T) valueMap.get(currentThread);
        if(value == null && !valueMap.containsKey(currentThread)) {
            value = initValue();
            valueMap.put(currentThread, value);
        }
        return value;
    }

    public void remove() {
        valueMap.remove(Thread.currentThread());
    }

    public T initValue() {
        return null;
    }
}

应用场景:

web应用分层结构中:同一变量在持久化,业务,展示层传递时(同一线程)。

接口方法:

    protected T initialValue(); //return the initial value for this thread-local
    public T get(); //return the current thread's value of this thread-local
    public void set(T value); //@param value the value to be stored in the current thread's copy of this thread-local.
    public void remove(); //Removes the current thread's value for this thread-local variable.

示例:

ThreadLocalContext.class
public class ThreadLocalContext {
    private static ThreadLocal<Integer> threadLocalNum = new ThreadLocal<Integer>() {
        @Override
        protected Integer initialValue() {
            return 0;
        }
    };

    public int getNextNum() {
        threadLocalNum.set(threadLocalNum.get()+1);
        return threadLocalNum.get();
    }
}
ThreadLocalTest.class
public class ThreadLocalTest {
    public static void main(String[] args) {
        ThreadLocalContext threadLocalContext  = new ThreadLocalContext();
        WorkThread workThread1 = new WorkThread(threadLocalContext);
        WorkThread workThread2 = new WorkThread(threadLocalContext);
        WorkThread workThread3 = new WorkThread(threadLocalContext);
        workThread1.start();
        workThread2.start();
        workThread3.start();
    }

}

class WorkThread extends Thread{
    private ThreadLocalContext threadLocalContext;
    public WorkThread(ThreadLocalContext threadLocalContext) {
        this.threadLocalContext = threadLocalContext;
    }

    public void run() {
        for(int i = 0; i < 5; i++) {
            System.out.println("thread[" + Thread.currentThread().getName()
                    + "] threadLocalNum [" + threadLocalContext.getNextNum() + "]");
        }
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值