Search in sources :

Example 1 with PeriodicDirectTask

use of io.reactivex.rxjava3.core.Scheduler.PeriodicDirectTask in project RxJava by ReactiveX.

the class Scheduler method schedulePeriodicallyDirect.

/**
 * Schedules a periodic execution of the given task with the given initial time delay and repeat period.
 *
 * <p>
 * This method is safe to be called from multiple threads but there are no
 * ordering guarantees between tasks.
 *
 * <p>
 * The periodic execution is at a fixed rate, that is, the first execution will be after the
 * {@code initialDelay}, the second after {@code initialDelay + period}, the third after
 * {@code initialDelay + 2 * period}, and so on.
 *
 * @param run the task to schedule
 * @param initialDelay the initial delay amount, non-positive values indicate non-delayed scheduling
 * @param period the period at which the task should be re-executed
 * @param unit the unit of measure of the delay amount
 * @return the Disposable that let's one cancel this particular delayed task.
 * @throws NullPointerException if {@code run} or {@code unit} is {@code null}
 * @since 2.0
 */
@NonNull
public Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, @NonNull TimeUnit unit) {
    final Worker w = createWorker();
    final Runnable decoratedRun = RxJavaPlugins.onSchedule(run);
    PeriodicDirectTask periodicTask = new PeriodicDirectTask(decoratedRun, w);
    Disposable d = w.schedulePeriodically(periodicTask, initialDelay, period, unit);
    if (d == EmptyDisposable.INSTANCE) {
        return d;
    }
    return periodicTask;
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable)

Example 2 with PeriodicDirectTask

use of io.reactivex.rxjava3.core.Scheduler.PeriodicDirectTask in project RxJava by ReactiveX.

the class PeriodicDirectTaskTest method runnableThrows.

@Test
public void runnableThrows() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Scheduler.Worker worker = Schedulers.single().createWorker();
        PeriodicDirectTask task = new PeriodicDirectTask(() -> {
            throw new TestException();
        }, worker);
        try {
            task.run();
            fail("Should have thrown!");
        } catch (TestException expected) {
        // expected
        }
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
        assertTrue(worker.isDisposed());
        task.run();
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) PeriodicDirectTask(io.reactivex.rxjava3.core.Scheduler.PeriodicDirectTask) Test(org.junit.Test)

Aggregations

PeriodicDirectTask (io.reactivex.rxjava3.core.Scheduler.PeriodicDirectTask)1 Disposable (io.reactivex.rxjava3.disposables.Disposable)1 TestException (io.reactivex.rxjava3.exceptions.TestException)1 Test (org.junit.Test)1