public Disposable schedulePeriodicallyDirect( Runnable run, long initialDelay, long period, TimeUnit unit) { ArrayCompositeResource<Disposable> acr = new ArrayCompositeResource<>(2, Disposable::dispose); Worker w = createWorker(); acr.lazySet(0, w); Runnable decoratedRun = RxJavaPlugins.onSchedule(run); acr.setResource( 1, w.schedulePeriodically( () -> { try { decoratedRun.run(); } catch (final Throwable e) { // make sure the worker is released if the run crashes acr.dispose(); throw e; } }, initialDelay, period, unit)); return acr; }
/** * Schedules the given runnable with the given delay directly on a worker of this scheduler. * * <p>Override this method to provide an efficient implementation that, for example, doesn't have * extra tracking structures for such one-shot executions. * * @param run * @param delay * @param unit * @return */ public Disposable scheduleDirect(Runnable run, long delay, TimeUnit unit) { Worker w = createWorker(); Runnable decoratedRun = RxJavaPlugins.onSchedule(run); w.schedule( () -> { try { decoratedRun.run(); } finally { w.dispose(); } }, delay, unit); return w; }