public class ThreadLocalDemo2 {
private static final ThreadLocal<SimpleDateFormat> THREAD_LOCAL = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
}
};
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(100);
for (int i = 0; i < 20; i++) {
service.execute(() -> {
for (int j = 0; j < 10; j++) {
try {
System.out.println(THREAD_LOCAL.get().parse("2020-03-12 20:54:12"));
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
service.shutdown();
}
}
09-11
3242
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-12
426
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-24
1814
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)