美文网首页
ThreadLocal

ThreadLocal

作者: kevin查_5ff1 | 来源:发表于2020-03-19 17:21 被阅读0次

ThreadLocal简介
       ThreadLocal是线程本地变量,是保证多线程并发数据安全的一种解决方法。当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量的线程创建独立的变量副本,所以每个线程操作该变量副本的时候,对其他线程各自的变量副本没有影响,因此达到了变量副本的隔离和安全。
ThreadLocal源码分析
    ThreadLocal变量和对象实例的映射关系是由
ThreadLocal使用场景
    多个线程去获取一个共享变量时,要求获取的是这个变量的初始值的副本。每个线程存储这个变量的副本,对变量的副本的改变不影响变量本身,适用于多个线程依赖不同变量值完成操作的场景。例如以下代码:

public class PrintThreadName{
    private String threadName;
    private String getThreadName(){
        return threadName;
    }
    private void setThreadName(String name){
        this.threadName = name;
    }
    public static main(){
        PrintThreadName print = new PrintThreadName();
        int threadCount = 9;
        CountDownLatch countDownLatch = new CountDownLatch(threadCount);
        for(int i = 0; i < threadCount; i++){

            new Thread(
                ()->{
                    print.setThreadName(Thread.currentThread().getName);
                    System.out.print(print.getThreadName());
                    countDown.countDown();
                },"thread-"+i).start();
        }
    }}

随机运行结果是: thread -1 thread -2 thread -1 thread -3 thread -4 thread -5 thread -6 thread -7 thread -8 Process finishedwithexitcode0 

public class PrintThreadName{
    ThreadLocal<String> threadName;
    private  String getThreadName(){
        threadName.get();
    }
    private void setThreadName(String name){
        threadName.set(name);
    }
    public static main(){       
        PrintThreadName print = new PrintThreadName();       
        int threadCount = 9;       
        CountDownLatch countDownLatch = new CountDownLatch(threadCount);        
            for(int i = 0; i < threadCount; i++){            
                new Thread(                ()->{                    
                    print.setThreadName(Thread.currentThread().getName);                                        
                    System.out.print(print.getThreadName());                                       
                    countDown.countDown();                
                },"thread-"+i).start();        
            }   
        }
}
随机运行结果是:
 thread-0 thread -1thread -2thread -3 thread -4 thread -5 thread -6 thread -7 thread-8
Process 

ThreadLocal源码分析
对象实例与ThreadLocal变量的映射关系是由线程Thread来维护,对象实例与ThreadLocal变量的映射关系是存放在ThreadLocalMap里。
public void set(T value){
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);
    if(map != null){
        map.set(this,value);
    }else{
        createMap(t,value);
    }
}
ThreadLocalMap getMap(Thread t){
    return t.threadLocals;
}
void createMap(){
    t.threadLocals = new ThreadLocalMap(this, firstValue);
}

public T get(){
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);
    if(map != null){
        ThreadLocalMap.Entry  e = map.getEntry(this);
        if(e != null){
            T result = (T)e.value;
            return result;
        }
    }
    return setInitialValue();
}
private T setInitialValue(){
        T value = initialValue();
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if(map != null){
            map.set(this,value);
        }else{
            createMap(t,map);
        }
}

相关文章

网友评论

      本文标题:ThreadLocal

      本文链接:https://www.haomeiwen.com/subject/lzjlyhtx.html