ThreadLocal的用法

    Java中除了通过共享变量,还可以通过ThreadLocal来实现多线程访问的问题。按字面理解,ThreadLocal相当于线程的局部变量 。在每个线程中为目标对象创建一个副本,这样自然避免了共享变量参数传递带来的线程安全问题。我们来做个测试:
package test.thread;

public class ThreadLocalTest1 {

	private static ThreadLocal<Object> threadLocal = new ThreadLocal<Object>();

	public static Object getInstance() {
		Object o = threadLocal.get();
		if (null == o) {
			o = new Object();
			threadLocal.set(o);
		}
		return o;
	}

	public static void main(String[] args) {

		Runnable threadImp = new Runnable() {

			@Override
			public void run() {
				Object o = getInstance();
				System.out.println(o);
			}
		};
		Thread[] threads = new Thread[10];
		for (int i = 0; i < threads.length; i++) {
			threads[i] = new Thread(threadImp);
			threads[i].start();
		}
	}
}

    最后打印结果:

java.lang.Object@6f92c766
java.lang.Object@4f9bbd86
java.lang.Object@d6132c4
java.lang.Object@d6132c4
java.lang.Object@5cb08ba7
java.lang.Object@130a7be0
java.lang.Object@3918d722
java.lang.Object@4b0bc3c9
java.lang.Object@1453ecec
java.lang.Object@11e78461

    上面结果显然印证了前面所述。那ThreadLocal是如何实现的呢?我们来看来下源码:

    /**
     * Returns the value in the current thread's copy of this
     * thread-local variable.  If the variable has no value for the
     * current thread, it is first initialized to the value returned
     * by an invocation of the {@link #initialValue} method.
     *
     * @return the current thread's value of this thread-local
     */
    public T get() {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null) {
            ThreadLocalMap.Entry e = map.getEntry(this);
            if (e != null)
                return (T)e.value;
        }
        return setInitialValue();
    }

    /**
     * Sets the current thread's copy of this thread-local variable
     * to the specified value.  Most subclasses will have no need to
     * override this method, relying solely on the {@link #initialValue}
     * method to set the values of thread-locals.
     *
     * @param value the value to be stored in the current thread's copy of
     *        this thread-local.
     */
    public void set(T value) {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null)
            map.set(this, value);
        else
            createMap(t, value);
    }

    /**
     * Get the map associated with a ThreadLocal. Overridden in
     * InheritableThreadLocal.
     *
     * @param  t the current thread
     * @return the map
     */
    ThreadLocalMap getMap(Thread t) {
        return t.threadLocals;
    }

    从get方法可以看出ThreadLocal所属的线程会维系一个ThreadLocalMap,最终取出的对象就存放在key为当前ThreadLocal对象的entry中,默认返回个null值。

    ThreadLocal多用于多线程创建线程内单例,如Session、Connection等。Spring、Hibernate中就有很多单例使用ThreadLocal实现。

参考:

ThreadLocal使用,应用场景,源码实现,内存泄漏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值