ThreadLocal作用类似于线程中的局部变量,单可以在外部进行引用,赋值,其他线程访问不到这个对象引用,可以看出每个线程私有的变量。在一个给定线程中首次调用 get 时, 会调用 initialValue 方法。在此之后,get 方法会返回属于当前线程的那个实例。或者当线程运行中执行set后,同一个线程get后返回的是同一个对象。通过源代码可以得知使用了弱引用-当gc回收后对象也回收(Thread中有个map ThreadLocalMap 中的Entry是个对ThreadLocal对象的弱引用,map的键是ThreadLocal 值是传入的value),一般对象引用是强引用,还有软引用,gc后仍不消失,只要弱引用对象(WeakReference)对应的引用(weakObject)不消失可以通过weakObject.get()方法获得队列中的对象(存入弱引用的值),可以作为缓存来使用。虚引用-第一次get也获得不到,作为堆外对象的管理使用。
package com.example.test;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class TestThreadLocal {
public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
public static final ThreadLocal<SimpleDateFormat> simpleDateFormatThreadLocal =
ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));
private static final int threadCount = 15;
private static Thread[] threads;
public static void main(String[] args) {
threads = new Thread[threadCount];
for (int i = 0; i < threadCount; i++) {
threads[i] = new Thread(() -> {
String dateStr = dateFormat.format(new Date());
System.out.println("dateFormat:" + dateStr);
dateFormat.setTimeZone(TimeZone.getTimeZone("Etc/GMT+11"));
String dateStr1 = simpleDateFormatThreadLocal.get().format(new Date());
System.out.println("simpleDateFormatThreadLocal:" + dateStr1);
});
threads[i].start();
}
}
}
测试代码
package com.example.test;
class TestThreadLocal implements AutoCloseable{
public static final ThreadLocal<Integer> integerThreadLocal=new ThreadLocal<>();
public void setThreadLocal(Integer value) {
integerThreadLocal.set(value);
}
public Integer getThreadLocal() {
return integerThreadLocal.get();
}
@Override
public void close() {
integerThreadLocal.remove();
}
}
public class MainClass{
public static void main(String[] args) throws InterruptedException {
try (TestThreadLocal testThreadLocal = new TestThreadLocal()) {
System.out.println("主线程开始");
System.out.println("主线程修改唯一的ThreadLocal对象的值为5");
testThreadLocal.setThreadLocal(5);
new Thread2().start();
while (true) {
System.out.println("main-如果互不影响将在main线程和Thread2线程看到不同的值 \n" +
testThreadLocal.getThreadLocal());
Thread.sleep(2000);
}
}
}
}
class Thread2 extends Thread {
@Override
public void run() {
try (TestThreadLocal testThreadLocal=new TestThreadLocal()){
System.out.println("Thread2 线程修改唯一的ThreadLocal对象的值为10");
testThreadLocal.setThreadLocal(10);
while (!isInterrupted()) {
try {
System.out.println("thread2-如果互不影响将在main线程和Thread2线程看到不同的值\n" +
+testThreadLocal.getThreadLocal());
Thread.sleep(2000);
}catch (InterruptedException E){
break;
}finally {
}
}
}
}
}
主线程开始
主线程修改唯一的ThreadLocal对象的值为5
main-如果互不影响将在main线程和Thread2线程看到不同的值
5
Thread2 线程修改唯一的ThreadLocal对象的值为10
thread2-如果互不影响将在main线程和Thread2线程看到不同的值
10
main-如果互不影响将在main线程和Thread2线程看到不同的值
5
thread2-如果互不影响将在main线程和Thread2线程看到不同的值
10
main-如果互不影响将在main线程和Thread2线程看到不同的值
5