取消并完成定期任务时的回调

Dre*_*rew 5 java

我有两个任务:第一个任务(work)重新出现,第二个任务(cleanup)释放一些资源.该cleanup任务应该正好运行一次重现的后work任务已经完成,并且不会再次运行.

我的第一直觉是这样的:

ScheduledExecutorService service = ...;
ScheduledFuture<?> future = service.scheduleAtFixedRate(work, ...);

// other stuff happens

future.cancel(false);
cleanup.run();
Run Code Online (Sandbox Code Playgroud)

这里的问题是cancel()立即返回.因此,如果work碰巧正在运行,那么cleanup将重叠它.

理想情况下,我会使用类似番石榴的东西Futures.addCallback(ListenableFuture future, FutureCallback callback).(番石榴15可能有类似的东西).

在此期间,如何在future取消并且 work不再运行时触发回调?

Dre*_*rew 1

这是我想出的解决方案。这似乎很简单,但我仍然认为有一个更常见和/或更优雅的解决方案。我真的很想在像番石榴这样的图书馆里看到一个......

首先,我创建一个包装器来对我的 Runnables 施加互斥:

private static final class SynchronizedRunnable implements Runnable {
    private final Object monitor;
    private final Runnable delegate;

    private SynchronizedRunnable(Object monitor, Runnable delegate) {
        this.monitor = monitor;
        this.delegate = delegate;
    }

    @Override
    public void run() {
        synchronized (monitor) {
            delegate.run();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我创建一个包装器来在成功调用时触发回调cancel

private static final class FutureWithCancelCallback<V> extends ForwardingFuture.SimpleForwardingFuture<V> {

    private final Runnable callback;

    private FutureWithCancelCallback(Future<V> delegate, Runnable callback) {
        super(delegate);
        this.callback = callback;
    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
            boolean cancelled = super.cancel(mayInterruptIfRunning);
            if (cancelled) {
                callback.run();
            }
            return cancelled;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我用我自己的方法将它们组合在一起:

private Future<?> scheduleWithFixedDelayAndCallback(ScheduledExecutorService service, Runnable work, long initialDelay, long delay, TimeUnit unit, Runnable cleanup) {

    Object monitor = new Object();

    Runnable monitoredWork = new SynchronizedRunnable(monitor, work);

    Runnable monitoredCleanup = new SynchronizedRunnable(monitor, cleanup);

    Future<?> rawFuture = service.scheduleAtFixedRate(monitoredWork, initialDelay, delay, unit);

    Future<?> wrappedFuture = new FutureWithCancelCallback(rawFuture, monitoredCleanup);

    return wrappedFuture;
}
Run Code Online (Sandbox Code Playgroud)