java源码学习4-ThreadLocal

1、ThreadLocal 介绍:

看jdk 源码说明:

 * This class provides thread-local variables.  These variables differ from
 * their normal counterparts in that each thread that accesses one (via its
 * <tt>get</tt> or <tt>set</tt> method) has its own, independently initialized
 * copy of the variable.  <tt>ThreadLocal</tt> instances are typically private
 * static fields in classes that wish to associate state with a thread (e.g.,
 * a user ID or Transaction ID).

ThreadLocal,很多地方叫做线程本地变量,也有些地方叫做线程本地存储,其实意思差不多。ThreadLocal为变量在每个线程中都创建了一个拷贝,那么每个线程可以访问自己内部的副本变量。ThreadLocal典型的应用是   作为一个静态变量与多线程访问的目标对象进行关联,看JDK 自带的举例:

 * public class ThreadId {
 *     // Atomic integer containing the next thread ID to be assigned
 *     private static final AtomicInteger nextId = new AtomicInteger(0);
 *
 *     // Thread local variable containing each thread's ID
 *     private static final ThreadLocal<Integer> threadId =
 *         new ThreadLocal<Integer>() {
 *             @Override protected Integer initialValue() {
 *                 return nextId.getAndIncrement();
 *         }
 *     };
 *
 *     // Returns the current thread's unique ID, assigning it if necessary
 *     public static int get() {
 *         return threadId.get();
 *     }
 * }
把代码补充完成,如下:

public class TestTreadLocal {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyThread t1=new MyThread();
		MyThread t2=new MyThread();
		MyThread t3=new MyThread();
		t1.start();t2.start();t3.start();
	}

}
class ThreadId{

		     private static final AtomicInteger nextId = new AtomicInteger(0);
		      // Thread local variable containing each thread's ID
		     private static final ThreadLocal<Integer> threadId =new ThreadLocal<Integer>() {
		              @Override 
		              protected Integer initialValue() {
		                  return nextId.getAndIncrement();
		         }
		      };
		 
		      // Returns the current thread's unique ID, assigning it if necessary
		      public static int get() {
		          return threadId.get();
		      }

}

class MyThread extends Thread{
	 ThreadId t=new ThreadId();//无论创建多少个对象,因为里面的变量是static 的,只会创建一次
	public void run(){
		System.out.println(Thread.currentThread().getName()+"     "+t.get());
	}
	
}
分析一下代码,多个线程与ID进行绑定,在每个线程启动的时候,调用ThreadId的get 方法,该方法本来是返回一个ID,但是这里使用了ThreadLocal 对象,并且在创建ThreadLocal对象的时候重写了初始化方法,返回ID当前值并且自增。

查看ThreadLocal的get 方法

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();
    }

首先判断当前线程对象在ThreadLocalMap里是否存在,如果存在,则返回value,如果不存在,调用setInitialValue方法

进一步查看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, value);
        return value;
    }
首先调用初始化方法,由于我们在创建ThreadLocal 对象的时候重写了该方法,所以这里的value首先是0(可以理解为第一个线程对象启动),然后判断当前线程对象是否在ThreadLocalMap里存在,如果存在,则绑定线程对象与值(0),如果不存在,则创建,重新绑定,返回值数据(0);


 目前由于基础比较薄弱,对于该类的使用不甚了了,所以从网上找了几篇文章,关于ThreadLocal 的使用与理解,可以用来参考

1、http://www.cnblogs.com/dolphin0520/p/3920407.html

2、http://www.iteye.com/topic/103804

3、http://www.cnblogs.com/alphablox/archive/2013/01/20/2869061.html

4、http://blog.csdn.net/lufeng20/article/details/24314381/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值