io.reactivex.rxjava3.internal.schedulers.ExecutorScheduler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava Show documentation
Show all versions of rxjava Show documentation
Reactive Extensions for Java
/*
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.rxjava3.internal.schedulers;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Scheduler;
import io.reactivex.rxjava3.disposables.*;
import io.reactivex.rxjava3.internal.disposables.*;
import io.reactivex.rxjava3.internal.functions.Functions;
import io.reactivex.rxjava3.internal.queue.MpscLinkedQueue;
import io.reactivex.rxjava3.internal.schedulers.ExecutorScheduler.ExecutorWorker.*;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import io.reactivex.rxjava3.schedulers.*;
/**
* Wraps an Executor and provides the Scheduler API over it.
*/
public final class ExecutorScheduler extends Scheduler {
final boolean interruptibleWorker;
final boolean fair;
@NonNull
final Executor executor;
static final class SingleHolder {
static final Scheduler HELPER = Schedulers.single();
}
public ExecutorScheduler(@NonNull Executor executor, boolean interruptibleWorker, boolean fair) {
this.executor = executor;
this.interruptibleWorker = interruptibleWorker;
this.fair = fair;
}
@NonNull
@Override
public Worker createWorker() {
return new ExecutorWorker(executor, interruptibleWorker, fair);
}
@NonNull
@Override
public Disposable scheduleDirect(@NonNull Runnable run) {
Runnable decoratedRun = RxJavaPlugins.onSchedule(run);
try {
if (executor instanceof ExecutorService) {
ScheduledDirectTask task = new ScheduledDirectTask(decoratedRun, interruptibleWorker);
Future> f = ((ExecutorService)executor).submit(task);
task.setFuture(f);
return task;
}
if (interruptibleWorker) {
InterruptibleRunnable interruptibleTask = new InterruptibleRunnable(decoratedRun, null);
executor.execute(interruptibleTask);
return interruptibleTask;
} else {
BooleanRunnable br = new BooleanRunnable(decoratedRun);
executor.execute(br);
return br;
}
} catch (RejectedExecutionException ex) {
RxJavaPlugins.onError(ex);
return EmptyDisposable.INSTANCE;
}
}
@NonNull
@Override
public Disposable scheduleDirect(@NonNull Runnable run, final long delay, final TimeUnit unit) {
final Runnable decoratedRun = RxJavaPlugins.onSchedule(run);
if (executor instanceof ScheduledExecutorService) {
try {
ScheduledDirectTask task = new ScheduledDirectTask(decoratedRun, interruptibleWorker);
Future> f = ((ScheduledExecutorService)executor).schedule(task, delay, unit);
task.setFuture(f);
return task;
} catch (RejectedExecutionException ex) {
RxJavaPlugins.onError(ex);
return EmptyDisposable.INSTANCE;
}
}
final DelayedRunnable dr = new DelayedRunnable(decoratedRun);
Disposable delayed = SingleHolder.HELPER.scheduleDirect(new DelayedDispose(dr), delay, unit);
dr.timed.replace(delayed);
return dr;
}
@NonNull
@Override
public Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, TimeUnit unit) {
if (executor instanceof ScheduledExecutorService) {
Runnable decoratedRun = RxJavaPlugins.onSchedule(run);
try {
ScheduledDirectPeriodicTask task = new ScheduledDirectPeriodicTask(decoratedRun, interruptibleWorker);
Future> f = ((ScheduledExecutorService)executor).scheduleAtFixedRate(task, initialDelay, period, unit);
task.setFuture(f);
return task;
} catch (RejectedExecutionException ex) {
RxJavaPlugins.onError(ex);
return EmptyDisposable.INSTANCE;
}
}
return super.schedulePeriodicallyDirect(run, initialDelay, period, unit);
}
/* public: test support. */
public static final class ExecutorWorker extends Scheduler.Worker implements Runnable {
final boolean interruptibleWorker;
final boolean fair;
final Executor executor;
final MpscLinkedQueue queue;
volatile boolean disposed;
final AtomicInteger wip = new AtomicInteger();
final CompositeDisposable tasks = new CompositeDisposable();
public ExecutorWorker(Executor executor, boolean interruptibleWorker, boolean fair) {
this.executor = executor;
this.queue = new MpscLinkedQueue<>();
this.interruptibleWorker = interruptibleWorker;
this.fair = fair;
}
@NonNull
@Override
public Disposable schedule(@NonNull Runnable run) {
if (disposed) {
return EmptyDisposable.INSTANCE;
}
Runnable decoratedRun = RxJavaPlugins.onSchedule(run);
Runnable task;
Disposable disposable;
if (interruptibleWorker) {
InterruptibleRunnable interruptibleTask = new InterruptibleRunnable(decoratedRun, tasks);
tasks.add(interruptibleTask);
task = interruptibleTask;
disposable = interruptibleTask;
} else {
BooleanRunnable runnableTask = new BooleanRunnable(decoratedRun);
task = runnableTask;
disposable = runnableTask;
}
queue.offer(task);
if (wip.getAndIncrement() == 0) {
try {
executor.execute(this);
} catch (RejectedExecutionException ex) {
disposed = true;
queue.clear();
RxJavaPlugins.onError(ex);
return EmptyDisposable.INSTANCE;
}
}
return disposable;
}
@NonNull
@Override
public Disposable schedule(@NonNull Runnable run, long delay, @NonNull TimeUnit unit) {
if (delay <= 0) {
return schedule(run);
}
if (disposed) {
return EmptyDisposable.INSTANCE;
}
SequentialDisposable first = new SequentialDisposable();
final SequentialDisposable mar = new SequentialDisposable(first);
final Runnable decoratedRun = RxJavaPlugins.onSchedule(run);
ScheduledRunnable sr = new ScheduledRunnable(new SequentialDispose(mar, decoratedRun), tasks);
tasks.add(sr);
if (executor instanceof ScheduledExecutorService) {
try {
Future> f = ((ScheduledExecutorService)executor).schedule((Callable
© 2015 - 2024 Weber Informatics LLC | Privacy Policy