背景:异步线程执行excel上传oss后资源未释放。内存随着上传次数逐渐升高。
解决:上传操作结束清除当前线程local。
/**
* 上海xx信息科技有限公司
*
* @author JinLi
* @description
* @email jinli@theduapp.com
* @date 2020/6/17
*/
public class ThreadLocalUtil {
/**
* 清除当前线程的threadLocals
*/
public static void cleanCurrentThreadLocal() {
Thread currentThread = Thread.currentThread();
try {
Field field = Thread.class.getDeclaredField("threadLocals");
field.setAccessible(true);
Object threadLocals = field.get(currentThread);
if (threadLocals != null) {
field.set(currentThread, null);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
}