ThreadLocal使用中记录
初始化ThreadLocal赋值
写法一
private ThreadLocal<Student> localStudent = new ThreadLocal<Student>() {
@Override
protected Student initialValue() {
return new Student();
}
};
写法二
Lambda表达式
private ThreadLocal<Student> localStudent = ThreadLocal.withInitial(Student::new);
获取ThreadLocal
localStudent.get();
ThreadLocal的remove
ThreadLocal必须在结束时使用remove方法,尤其是在线程池的使用时不仅会造成内存泄露,还会导致逻辑上问题