ThreadLocal 是什么
- ThreadLocal是线程本地变量,也就是每一个线程有对应的从ThreadLocal中获取(调用get()方法)的变量,线程之间互不影响。
- ThreadLocal是一个带泛型的类,创建时一般要声明为public static final,而且还要重写initialValue()方法。
public static final ThreadLocal<Integer> THREAD_LOCAL = new ThreadLocal<Integer>(){
@Override
protected Integer initialValue() {
return 60;
}
};
- ThreadLocal中有两个常用的方法——get()和set(),get()是获取当前线程中变量的值,set()是赋值。
ThreadLocal类中的get()方法,根据当前线程获取ThreadLocalMap对象(ThreadLocal的内部类),再获取Entry对象(ThreadLocalMap的内部类),返回对象中的value
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
return setInitialValue();
}
代码示例
- 从下面的代码的运行结果可以看出线程t1和t2在操作THREAD_LOCAL 中的变量是互不影响的
public class RunnableTest implements Runnable {
public static final ThreadLocal<Integer> THREAD_LOCAL = new ThreadLocal<Integer>() {
@Override
protected Integer initialValue() {
return 60;
}
};
@Override
public void run() {
while (true) {
Integer tick = THREAD_LOCAL.get();
if (tick == 0) {
break;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "=========" + tick);
THREAD_LOCAL.set(--tick);
}
}
public static void main(String[] args) throws InterruptedException {
RunnableTest runnableTest = new RunnableTest();
Thread t1 = new Thread(runnableTest);
Thread t2 = new Thread(runnableTest);
t1.start();
t2.start();
}
}
运行结果
Thread-1=========60
Thread-0=========60
Thread-0=========59
Thread-1=========59
Thread-0=========58
Thread-1=========58
Thread-0=========57
Thread-1=========57
Thread-1=========56
Thread-0=========56
Thread-0=========55
Thread-1=========55
Thread-1=========54
Thread-0=========54
Thread-0=========53
Thread-1=========53
Thread-0=========52
Thread-1=========52
Thread-0=========51
Thread-1=========51
Thread-1=========50
Thread-0=========50
Thread-1=========49
Thread-0=========49
Thread-1=========48
Thread-0=========48
Thread-0=========47
Thread-1=========47
Thread-0=========46
Thread-1=========46
Thread-1=========45
Thread-0=========45
Thread-1=========44
Thread-0=========44
Thread-1=========43
Thread-0=========43
Thread-0=========42
Thread-1=========42
Thread-0=========41
Thread-1=========41
Thread-1=========40
Thread-0=========40
Thread-1=========39
Thread-0=========39
Thread-0=========38
Thread-1=========38
Thread-1=========37
Thread-0=========37
Thread-1=========36
Thread-0=========36
Thread-1=========35
Thread-0=========35
Thread-1=========34
Thread-0=========34
Thread-1=========33
Thread-0=========33
Thread-1=========32
Thread-0=========32
Thread-1=========31
Thread-0=========31
Thread-1=========30
Thread-0=========30
Thread-1=========29
Thread-0=========29
Thread-1=========28
Thread-0=========28
Thread-1=========27
Thread-0=========27
Thread-1=========26
Thread-0=========26
Thread-1=========25
Thread-0=========25
Thread-1=========24
Thread-0=========24
Thread-1=========23
Thread-0=========23
Thread-1=========22
Thread-0=========22
Thread-1=========21
Thread-0=========21
Thread-1=========20
Thread-0=========20
Thread-1=========19
Thread-0=========19
Thread-1=========18
Thread-0=========18
Thread-1=========17
Thread-0=========17
Thread-1=========16
Thread-0=========16
Thread-1=========15
Thread-0=========15
Thread-1=========14
Thread-0=========14
Thread-1=========13
Thread-0=========13
Thread-0=========12
Thread-1=========12
Thread-1=========11
Thread-0=========11
Thread-1=========10
Thread-0=========10
Thread-1=========9
Thread-0=========9
Thread-1=========8
Thread-0=========8
Thread-1=========7
Thread-0=========7
Thread-1=========6
Thread-0=========6
Thread-1=========5
Thread-0=========5
Thread-1=========4
Thread-0=========4
Thread-1=========3
Thread-0=========3
Thread-1=========2
Thread-0=========2
Thread-1=========1
Thread-0=========1
源码地址:https://github.com/jiangxlGit/steady/tree/master/steady