欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ThreadLocal理解

程序员文章站 2022-06-28 19:21:23
ThreadLocal 概述 ThreadLocal实例仅作为线程局部变量的==操作类==,以及==线程存储局部变量时的Key==。真正的线程局部变量是存储在各自线程的本地,通过Thread类中的 进行存储。 若希望在线程本地存储多个局部变量需要使用多个ThreadLocal实例进行操作。 Thre ......

threadlocal

概述

threadlocal实例仅作为线程局部变量的==操作类==,以及==线程存储局部变量时的key==。真正的线程局部变量是存储在各自线程的本地,通过thread类中的threadlocal.threadlocalmap threadlocals进行存储。

若希望在线程本地存储多个局部变量需要使用多个threadlocal实例进行操作。

threadlocal源码示例

  • set(t v)方法

    通过thread.currentthread()获取当前的线程实例,然后获取当前线程实例里的threadlocal.threadlocalmap threadlocals,若不存在该集合对象则创建。最后,将当前threadlocal实例作为key,将参数值v存储当前线程的threadlocals集合中。即var3.set(this, var1);

  • get()方法

    通过thread.currentthread()获取当前的线程实例,然后获取当前线程实例里的threadlocal.threadlocalmap threadlocals,若该集合对象不为空,则用当前threadlocal实例作为key,从集合对象中获取之前存储的值。即var3 = var2.getentry(this);

    public t get() {
        thread var1 = thread.currentthread();
        threadlocal.threadlocalmap var2 = this.getmap(var1);
        if (var2 != null) {
            threadlocal.threadlocalmap.entry var3 = var2.getentry(this);
            if (var3 != null) {
                object var4 = var3.value;
                return var4;
            }
        }

        return this.setinitialvalue();
    }

    private t setinitialvalue() {
        object var1 = this.initialvalue();
        thread var2 = thread.currentthread();
        threadlocal.threadlocalmap var3 = this.getmap(var2);
        if (var3 != null) {
            var3.set(this, var1);
        } else {
            this.createmap(var2, var1);
        }

        return var1;
    }

    public void set(t var1) {
        thread var2 = thread.currentthread();
        threadlocal.threadlocalmap var3 = this.getmap(var2);
        if (var3 != null) {
            var3.set(this, var1);
        } else {
            this.createmap(var2, var1);
        }

    }

    public void remove() {
        threadlocal.threadlocalmap var1 = this.getmap(thread.currentthread());
        if (var1 != null) {
            var1.remove(this);
        }

    }

    threadlocal.threadlocalmap getmap(thread var1) {
        return var1.threadlocals;
    }

    void createmap(thread var1, t var2) {
        var1.threadlocals = new threadlocal.threadlocalmap(this, var2);
    }

thread源码示例

thread类使用threadlocal.threadlocalmap threadlocals集合对象盛装线程局部变量。

    public class thread implements runnable {
    /* make sure registernatives is the first thing <clinit> does. */
    private static native void registernatives();
    static {
        registernatives();
    }
    private volatile string name;
    private int            priority;
    private thread         threadq;
    private long           eetop;
    /* whether or not to single_step this thread. */
    private boolean     single_step;
    /* whether or not the thread is a daemon thread. */
    private boolean     daemon = false;
    /* jvm state */
    private boolean     stillborn = false;
    /* what will be run. */
    private runnable target;
    /* the group of this thread */
    private threadgroup group;
    /* the context classloader for this thread */
    private classloader contextclassloader;
    /* the inherited accesscontrolcontext of this thread */
    private accesscontrolcontext inheritedaccesscontrolcontext;
    /* for autonumbering anonymous threads. */
    private static int threadinitnumber;
    private static synchronized int nextthreadnum() {
        return threadinitnumber++;
    }
    /* threadlocal values pertaining to this thread. this map is maintained
     * by the threadlocal class. */
    threadlocal.threadlocalmap threadlocals = null;