Java ThreadLocal示例
时间:2020-02-23 14:36:58 来源:igfitidea点击:
Java ThreadLocal用于创建线程局部变量。
我们知道,对象的所有线程都共享其变量,因此该变量不是线程安全的。
我们可以使用同步来确保线程安全,但是如果要避免同步,可以使用ThreadLocal变量。
Java ThreadLocal
每个线程都有自己的" ThreadLocal"变量,他们可以使用它的get()和set()方法获取默认值或者将其本地值更改为Thread。
ThreadLocal实例通常是希望将状态与线程关联的类中的私有静态字段。
Java ThreadLocal示例
这是一个小示例,展示了在Java程序中使用ThreadLocal并证明每个线程都有其自己的ThreadLocal变量副本。
ThreadLocalExample.java
package com.theitroad.threads;
import java.text.SimpleDateFormat;
import java.util.Random;
public class ThreadLocalExample implements Runnable{
//SimpleDateFormat is not thread-safe, so give one to each thread
private static final ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>(){
@Override
protected SimpleDateFormat initialValue()
{
return new SimpleDateFormat("yyyyMMdd HHmm");
}
};
public static void main(String[] args) throws InterruptedException {
ThreadLocalExample obj = new ThreadLocalExample();
for(int i=0 ; i<10; i++){
Thread t = new Thread(obj, ""+i);
Thread.sleep(new Random().nextInt(1000));
t.start();
}
}
@Override
public void run() {
System.out.println("Thread Name= "+Thread.currentThread().getName()+" default Formatter = "+formatter.get().toPattern());
try {
Thread.sleep(new Random().nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
//formatter pattern is changed here by thread, but it won't reflect to other threads
formatter.set(new SimpleDateFormat());
System.out.println("Thread Name= "+Thread.currentThread().getName()+" formatter = "+formatter.get().toPattern());
}
}
上面的java ThreadLocal示例程序的输出是:
Thread Name= 0 default Formatter = yyyyMMdd HHmm Thread Name= 1 default Formatter = yyyyMMdd HHmm Thread Name= 0 formatter = M/d/yy h:mm a Thread Name= 2 default Formatter = yyyyMMdd HHmm Thread Name= 1 formatter = M/d/yy h:mm a Thread Name= 3 default Formatter = yyyyMMdd HHmm Thread Name= 4 default Formatter = yyyyMMdd HHmm Thread Name= 4 formatter = M/d/yy h:mm a Thread Name= 5 default Formatter = yyyyMMdd HHmm Thread Name= 2 formatter = M/d/yy h:mm a Thread Name= 3 formatter = M/d/yy h:mm a Thread Name= 6 default Formatter = yyyyMMdd HHmm Thread Name= 5 formatter = M/d/yy h:mm a Thread Name= 6 formatter = M/d/yy h:mm a Thread Name= 7 default Formatter = yyyyMMdd HHmm Thread Name= 8 default Formatter = yyyyMMdd HHmm Thread Name= 8 formatter = M/d/yy h:mm a Thread Name= 7 formatter = M/d/yy h:mm a Thread Name= 9 default Formatter = yyyyMMdd HHmm Thread Name= 9 formatter = M/d/yy h:mm a
从输出中可以看到,Thread-0更改了格式化程序的值,但线程2默认格式化程序仍与初始化值相同。
您也可以在其他线程中看到相同的模式。
更新:ThreadLocal类在Java 8中使用新方法withInitial()进行了扩展,该方法以Supplier功能接口作为参数。
因此,我们可以使用lambda表达式轻松创建ThreadLocal实例。
例如,可以在以下一行中定义上述格式化程序ThreadLocal变量:
private static final ThreadLocal<SimpleDateFormat> formatter =
ThreadLocal.<SimpleDateFormat>withInitial
(() -> {return new SimpleDateFormat("yyyyMMdd HHmm");});

