想执行某一代码指定次数 每次执行间隔一段时间
        ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor();
        pool.scheduleWithFixedDelay(() -> {
            System.out.println("doSomeTask");
        }, 1, 1, TimeUnit.SECONDS);但这种方式没办法指定执行次数, 如指定执行3次, 执行三次后就结束。
只能用下面的方式
        CompletableFuture.supplyAsync(() -> {
            for(int i=0; i<3; i++){
                System.out.println("doSomeTask");
                try { Thread.sleep(1000); } catch (Exception e) { }
            }
            return null;
        });除了这种方式外可有更简便的方式指定定时任务执行指定次数?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
将你上面的写法抽象出来,写个静态方法,用的时候,import static...