如何在执行一定次数后停止计划重复执行的 Runnable

2024-02-18

情况

我有一个可运行的。我有一个类,它使用 ScheduledExecutorService 来安排此 Runnable 执行固定延迟调度 http://download.oracle.com/javase/1,5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html#scheduleWithFixedDelay%28java.lang.Runnable,%20long,%20long,%20java.util.concurrent.TimeUnit%29.

Goal

我想更改此类以安排可运行的固定延迟执行either无限期地,or直到它运行一定次数,具体取决于传递给构造函数的某些参数。

如果可能的话,我想使用相同的 Runnable,因为它在概念上是应该“运行”的相同事物。

可能的方法

方法#1

有两个 Runnable,一个在多次执行后取消计划(它会保留计数),另一个则不会:

public class MyClass{
    private ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

    public enum Mode{
        INDEFINITE, FIXED_NO_OF_TIMES
    }

    public MyClass(Mode mode){
        if(mode == Mode.INDEFINITE){
            scheduler.scheduleWithFixedDelay(new DoSomethingTask(), 0, 100, TimeUnit.MILLISECONDS);
        }else if(mode == Mode.FIXED_NO_OF_TIMES){
            scheduler.scheduleWithFixedDelay(new DoSomethingNTimesTask(), 0, 100, TimeUnit.MILLISECONDS);
        }
    }

    private class DoSomethingTask implements Runnable{
        @Override
        public void run(){
            doSomething();
        }
    }

    private class DoSomethingNTimesTask implements Runnable{
        private int count = 0;

        @Override
        public void run(){
            doSomething();
            count++;
            if(count > 42){
                // Cancel the scheduling.
                // Can you do this inside the run method, presumably using
                // the Future returned by the schedule method? Is it a good idea?
            }
        }
    }

    private void doSomething(){
        // do something
    }
}

我宁愿只有一个 Runnable 来执行 doSomething 方法。将调度与 Runnable 联系起来感觉是错误的。你怎么看待这件事?

方法#2

有一个 Runnable 来执行我们想要定期运行的代码。有一个单独的预定可运行对象,用于检查第一个可运行对象已运行多少次,并在达到一定数量时取消。这可能不准确,因为它是异步的。感觉有点麻烦。你怎么看待这件事?

方法#3

扩展 ScheduledExecutorService 并添加方法“scheduleWithFixedDelayNTimes”。也许这样的类已经存在了?目前,我正在使用Executors.newSingleThreadScheduledExecutor();获取我的 ScheduledExecutorService 实例。我可能必须实现类似的功能来实例化扩展的 ScheduledExecutorService。这可能很棘手。你怎么看待这件事?

无调度程序方法[编辑]

我无法使用调度程序。我可以有类似的东西:

for(int i = 0; i < numTimesToRun; i++){
    doSomething();
    Thread.sleep(delay);
}

并在某个线程中运行它。你对那个怎么想的?您仍然可以使用 runnable 并直接调用 run 方法。


欢迎任何建议。我正在寻求一场辩论,以找到实现我的目标的“最佳实践”方式。


您可以在 Future 上使用 cancel() 方法。来自 javadoc固定费率安排 http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html#scheduleAtFixedRate%28java.lang.Runnable,%20long,%20long,%20java.util.concurrent.TimeUnit%29

Otherwise, the task will only terminate via cancellation or termination of the executor

下面是一些示例代码,它将一个 Runnable 包装在另一个 Runnable 中,跟踪原始运行的次数,并在运行 N 次后取消。

public void runNTimes(Runnable task, int maxRunCount, long period, TimeUnit unit, ScheduledExecutorService executor) {
    new FixedExecutionRunnable(task, maxRunCount).runNTimes(executor, period, unit);
}

class FixedExecutionRunnable implements Runnable {
    private final AtomicInteger runCount = new AtomicInteger();
    private final Runnable delegate;
    private volatile ScheduledFuture<?> self;
    private final int maxRunCount;

    public FixedExecutionRunnable(Runnable delegate, int maxRunCount) {
        this.delegate = delegate;
        this.maxRunCount = maxRunCount;
    }

    @Override
    public void run() {
        delegate.run();
        if(runCount.incrementAndGet() == maxRunCount) {
            boolean interrupted = false;
            try {
                while(self == null) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        interrupted = true;
                    }
                }
                self.cancel(false);
            } finally {
                if(interrupted) {
                    Thread.currentThread().interrupt();
                }
            }
        }
    }

    public void runNTimes(ScheduledExecutorService executor, long period, TimeUnit unit) {
        self = executor.scheduleAtFixedRate(this, 0, period, unit);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在执行一定次数后停止计划重复执行的 Runnable 的相关文章

随机推荐