前端时间用lombok 的@Cleanup() 想实现线程池的自动关闭,因为使用不当,查bug查了好久,因此写篇博客纪念下,同时也希望读者可以跳过这个坑。
@Cleanup修饰的对象,可以在对象资源使用结束后,自动关闭。
1、错误的用法
@Test
public void test() {
ThreadPoolExecutor executor = createExecutor();
System.out.println(executor);
}
private ThreadPoolExecutor createExecutor() {
@Cleanup("shutdown") ThreadPoolExecutor threadPoolExecutor

本文讲述了如何避免在使用Lombok的@Cleanup功能时导致线程池意外关闭的问题,包括错误用法示例、正确用法以及注意事项,强调资源对象需符合Closeable接口和正确作用域的要求。
最低0.47元/天 解锁文章
1026

被折叠的 条评论
为什么被折叠?



