@Cleanup() 使用注意事项

编程入门 行业动态 更新时间:2024-10-26 22:22:30

@Cleanup() 使用<a href=https://www.elefans.com/category/jswz/34/1768820.html style=注意事项"/>

@Cleanup() 使用注意事项

前端时间用lombok 的@Cleanup() 想实现线程池的自动关闭,因为使用不当,查bug查了好久,因此写篇博客纪念下,同时也希望读者可以跳过这个坑。

@Cleanup修饰的对象,可以在对象资源使用结束后,自动关闭。

1、错误的用法

    @Testpublic void test() {ThreadPoolExecutor executor = createExecutor();System.out.println(executor);}private ThreadPoolExecutor createExecutor() {@Cleanup("shutdown") ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 1L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1000), new ThreadFactoryBuilder().setNameFormat("testDemo" + "-thread-%d").build(), new ThreadPoolExecutor.CallerRunsPolicy());return threadPoolExecutor;}

上面是错误的用法,因为用 @Cleanup修饰了threadPoolExecutor,它会在这个方法块执行完毕后,自动执行线程池的shutdown方法,因此创建的线程池是一个关闭状态的。
debug 看下线程池状态:

2、正确的用法

 @Testpublic void test() {@Cleanup("shutdown") ThreadPoolExecutor executor = createExecutor();System.out.println(executor);}private ThreadPoolExecutor createExecutor() {ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 1L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1000), new ThreadFactoryBuilder().setNameFormat("testDemo" + "-thread-%d").build(), new ThreadPoolExecutor.CallerRunsPolicy());return threadPoolExecutor;}

debug看下线程池的状态:

3、其他注意事项

  1. @Cleanup 注解只能用于实现了 java.io.Closeable 接口的资源对象上。确保资源对象实现了 Closeable 接口,否则编译时会报错。

  2. @Cleanup 注解只能用于局部变量上,不能用于成员变量或静态变量上。

  3. 被 @Cleanup 注解修饰的资源对象会在代码块结束后自动调用其 close() 方法进行关闭。因此,确保资源对象在方法中的作用范围内,不要在方法外部使用该资源对象。

  4. @Cleanup 注解只能用于方法体内部,不能用于方法的参数上。

  5. @Cleanup 注解可以和其他注解一起使用,例如 @SneakyThrows 注解,用于抑制方法中可能抛出的异常。

  6. @Cleanup 注解不适用于所有场景,例如需要在 finally 块中进行其他操作的情况下,不应使用 @Cleanup 注解。

更多推荐

@Cleanup() 使用注意事项

本文发布于:2023-12-06 00:51:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1665931.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注意事项   Cleanup

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!