您的位置:首页 > 编程语言 > Java开发

java源码学习4-ThreadLocal

2016-09-18 16:42 309 查看
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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: