Java多线程编程五(线程间通信之ThreadLocal)
ThreadLocal
本文主要讲述如何解决非线程安全问题,感谢java多线程核心编程一书,为本系列文章提供参考借鉴
变量值的共享可以使用public static 变量的形式,所有的线程都是用同一个public static变量。如果想实现每一个线程都有自己共享变量该如何解决呢?JDK中提供的类ThreadLocal正式为了解决这样的问题。
类ThreadLocal主要解决的就是每个线程绑定自己的之,可以将ThreadLocal类比喻成全局存放数据的盒子,盒子中可以存储每个线程的私有数据。
方法 get()
eg:
public class ThreadMain {
public static ThreadLocal tl = new ThreadLocal();
public static void main (String[] args){
if (tl.get() == null) {
System.out.println("从未放过值");
tl.set("我的值");
}
System.out.println(tl.get());
System.out.println(tl.get());
}
}
运行结果为:
从结果来看,第一次调用tl对象的get()方法时返回的值是null,通过调用set()方法辅助后才能取出值来。
类ThreadLocal解决的是变量在不同线程间的隔离性,也就是不同线程拥有自己的值,不同线程中的值是可以放入ThreadLocal类中进行保存的。
如下:每个线程再set和get时都是自己所有的个人值
public class ThreadMain {
public static void main(String[] args) {
final ThreadLocal threadLocal = new ThreadLocal();
Thread thread1 = new Thread(new Runnable() {
@SuppressWarnings("all")
public void run() {
threadLocal.set("线程一得值");
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + "----" + threadLocal.get());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread1.setName("线程一");
thread1.start();
Thread thread2 = new Thread(new Runnable() {
@SuppressWarnings("all")
public void run() {
threadLocal.set("线程二得值");
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + "----" + threadLocal.get());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread2.setName("线程二");
thread2.start();
}
}
解决get()返回null
我们可以继承ThreadLocal重写其initialValue()方法,eg:
public class MyThreadLocal extends ThreadLocal {
@Override
protected Object initialValue() {
return "我的初始值";
}
}
InheritableThreadLocal的使用(值继承)
使用InheritableThreadLocal类可以让子线程从父线程中取得值。
public class InheritableThreadLocalExt extends InheritableThreadLocal {
@Override
protected Object initialValue() {
return "InheritableThreadLocalExt";
}
}
public class ThreadMain {
static InheritableThreadLocalExt inheritableThreadLocalExt = new InheritableThreadLocalExt();
public static void main(String[] args) {
try {
for (int i = 0; i < 10; i++) {
System.out.println(" 在Main线程中取值="+inheritableThreadLocalExt.get());
Thread.sleep(100);
}
Thread.sleep(2000);
inheritableThreadLocalExt.set("first change");
ThreadA threadA = new ThreadA();
threadA.start();
Thread.sleep(300);
inheritableThreadLocalExt.set("second change");
Thread.sleep(2000);
System.out.println("last value = "+inheritableThreadLocalExt.get());
new ThreadA().start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static class ThreadA extends Thread {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
System.out.println("在ThreadA线程中取值=" + inheritableThreadLocalExt.get());
Thread.sleep(100);
}
inheritableThreadLocalExt.set("change");
System.out.println("在ThreadA线程中取值=" + inheritableThreadLocalExt.get());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
结果:
有结果可知:子线程在调用get()方法时,若子线程中InheritableThreadLocal的初始值也就是继承得到的初始值永远是InheritableThreadLocal在父线程中当前的值。
继承值再修改
修改以上InheritableThreadLocalExt代码,重写方法childValue为:
public class InheritableThreadLocalExt extends InheritableThreadLocal {
@Override
protected Object initialValue() {
return "InheritableThreadLocalExt";
}
@Override
protected Object childValue(Object parentValue) {
return "child value";
}
}
其他不变运行结果为:
由结果可知:再重写childValue方法后,InheritableThreadLocal在子线程中的初始值都为childValue中返回的值,和父线程中InheritableThreadLocal的值无关。