@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;
    }

上面是错误的用法,因为用 @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;
    }

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

3、其他注意事项

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

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

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

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

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

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值