标签:java基础
ThreadLocal:只与当前线程绑定
InheritableThreadLocal:会把值传给子线程,与子线程共享值
//使用ThreadLocal,父子线程之间,不共享Value
final ThreadLocal<String> tl = new ThreadLocal<String>();
tl.set("ThreadLocal-VAL");
System.out.println("Main-1:" + tl.get());
new Thread() {
public void run() {
System.out.println("Child-1:" + tl.get());
};
}.start();
//使用InheritableThreadLocal,父线程Value可让子线程共享
final ThreadLocal<String> itl = new InheritableThreadLocal<String>();
itl.set("InheritableThreadLocal-VAL");
System.out.println("Main-2:" + itl.get());
new Thread() {
public void run() {
System.out.println("Child-2:" + itl.get());
};
}.start();
ThreadLocal与InheritableThreadLocal的区别
标签:java基础
原文地址:http://liushuiawen.blog.51cto.com/6961419/1731301