• @Cleanup() 使用注意事项


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

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

    1、错误的用法

        @Test
        public 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;
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

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

    2、正确的用法

     @Test
        public 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;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    debug看下线程池的状态:
    在这里插入图片描述

    3、其他注意事项

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

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

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

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

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

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

  • 相关阅读:
    数据仓库-数仓架构
    程序验证Jackson反序列化的规则、Jackson序列化与反序列化关键方法程序详细分析
    Java中集合知识点
    计算机毕业设计(75)php小程序毕设作品之网上销售小程序商城系统
    数据结构:KMP算法的原理图解和代码解析
    基础架构之分布式配置中心
    Linux python2 python3 切换
    Java注释:类、方法和字段注释
    Naivcat 数据迁移工具 | 竟然那么香
    A Survey and Framework of Cooperative Perception 论文阅读
  • 原文地址:https://blog.csdn.net/aaaPostcard/article/details/133962559