ThreadLocal保证线程安全:
ThreadLocal内部持有ThreadLocalMap对象,线程内部单独创建副本,来保证数据隔离,但是由于ThreadLocalMap中key为弱引用,GC自动回收,但是value如果为强引用,就没法回收,就会造成内存泄露(除非线程退出)
关于垃圾回收(自动回收堆中没引用的对象空间):https://www.jianshu.com/p/23f8249886c6
参考链接:https://www.cnblogs.com/jalon/p/14819372.html
/*
* 线程安全:
* 1.synchronized修饰
* 2.ThreadLocal修饰--数据隔离
*
* */
private ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>(){
@Override
protected SimpleDateFormat initialValue() {
// 这里会输出10次,分别是每个线程的id
System.out.println(Thread.currentThread().getId());
return new SimpleDateFormat("yyyy-MM-dd");
}
};
public void parse2(String dateString){
try {
System.out.println(threadLocal.get().parse(dateString));
} catch (ParseException e) {
e.printStackTrace();
}
}
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
public void parse(String dateString){
try {
synchronized (simpleDateFormat){
System.out.println(simpleDateFormat.parse(dateString));
}
} catch (ParseException e) {
e.printStackTrace();
}
}
@Test
void testThreadLocal(){
ExecutorService service = Executors.newFixedThreadPool(10);
for (int i = 0; i < 300; i++) {
service.submit(() -> {
// parse("2021-05-30");
parse2("2021-05-30");
});
}
}