| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.schedulers; |
| 15 |
|
| 16 |
import java.util.concurrent.*; |
| 17 |
import java.util.concurrent.atomic.AtomicReference; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 20 |
import io.reactivex.rxjava3.core.Scheduler; |
| 21 |
import io.reactivex.rxjava3.disposables.*; |
| 22 |
import io.reactivex.rxjava3.internal.disposables.EmptyDisposable; |
| 23 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 24 |
|
| 25 |
/** |
| 26 |
* A scheduler with a shared, single threaded underlying ScheduledExecutorService. |
| 27 |
* @since 2.0 |
| 28 |
*/ |
| 29 |
public final class SingleScheduler extends Scheduler { |
| 30 |
|
| 31 |
final ThreadFactory threadFactory; |
| 32 |
final AtomicReference<ScheduledExecutorService> executor = new AtomicReference<>(); |
| 33 |
|
| 34 |
/** The name of the system property for setting the thread priority for this Scheduler. */ |
| 35 |
private static final String KEY_SINGLE_PRIORITY = "rx3.single-priority"; |
| 36 |
|
| 37 |
private static final String THREAD_NAME_PREFIX = "RxSingleScheduler"; |
| 38 |
|
| 39 |
static final RxThreadFactory SINGLE_THREAD_FACTORY; |
| 40 |
|
| 41 |
static final ScheduledExecutorService SHUTDOWN; |
| 42 |
static { |
| 43 |
SHUTDOWN = Executors.newScheduledThreadPool(0); |
| 44 |
SHUTDOWN.shutdown(); |
| 45 |
|
| 46 |
int priority = Math.max(Thread.MIN_PRIORITY, Math.min(Thread.MAX_PRIORITY, |
| 47 |
Integer.getInteger(KEY_SINGLE_PRIORITY, Thread.NORM_PRIORITY))); |
| 48 |
|
| 49 |
SINGLE_THREAD_FACTORY = new RxThreadFactory(THREAD_NAME_PREFIX, priority, true); |
| 50 |
} |
| 51 |
|
| 52 |
public SingleScheduler() { |
| 53 |
this(SINGLE_THREAD_FACTORY); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Constructs a SingleScheduler with the given ThreadFactory and prepares the |
| 58 |
* single scheduler thread. |
| 59 |
* @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any |
| 60 |
* system properties for configuring new thread creation. Cannot be null. |
| 61 |
*/ |
| 62 |
public SingleScheduler(ThreadFactory threadFactory) { |
| 63 |
this.threadFactory = threadFactory; |
| 64 |
executor.lazySet(createExecutor(threadFactory)); |
| 65 |
} |
| 66 |
|
| 67 |
static ScheduledExecutorService createExecutor(ThreadFactory threadFactory) { |
| 68 |
return SchedulerPoolFactory.create(threadFactory); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void start() { |
| 73 |
ScheduledExecutorService next = null; |
| 74 |
for (;;) { |
| 75 |
ScheduledExecutorService current = executor.get(); |
| 76 |
if (current != SHUTDOWN) { |
| 77 |
if (next != null) { |
| 78 |
next.shutdown(); |
| 79 |
} |
| 80 |
return; |
| 81 |
} |
| 82 |
if (next == null) { |
| 83 |
next = createExecutor(threadFactory); |
| 84 |
} |
| 85 |
if (executor.compareAndSet(current, next)) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void shutdown() { |
| 94 |
ScheduledExecutorService current = executor.getAndSet(SHUTDOWN); |
| 95 |
if (current != SHUTDOWN) { |
| 96 |
current.shutdownNow(); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
@NonNull |
| 101 |
@Override |
| 102 |
public Worker createWorker() { |
| 103 |
return new ScheduledWorker(executor.get()); |
| 104 |
} |
| 105 |
|
| 106 |
@NonNull |
| 107 |
@Override |
| 108 |
public Disposable scheduleDirect(@NonNull Runnable run, long delay, TimeUnit unit) { |
| 109 |
ScheduledDirectTask task = new ScheduledDirectTask(RxJavaPlugins.onSchedule(run), true); |
| 110 |
try { |
| 111 |
Future<?> f; |
| 112 |
if (delay <= 0L) { |
| 113 |
f = executor.get().submit(task); |
| 114 |
} else { |
| 115 |
f = executor.get().schedule(task, delay, unit); |
| 116 |
} |
| 117 |
task.setFuture(f); |
| 118 |
return task; |
| 119 |
} catch (RejectedExecutionException ex) { |
| 120 |
RxJavaPlugins.onError(ex); |
| 121 |
return EmptyDisposable.INSTANCE; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
@NonNull |
| 126 |
@Override |
| 127 |
public Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, TimeUnit unit) { |
| 128 |
final Runnable decoratedRun = RxJavaPlugins.onSchedule(run); |
| 129 |
if (period <= 0L) { |
| 130 |
|
| 131 |
ScheduledExecutorService exec = executor.get(); |
| 132 |
|
| 133 |
InstantPeriodicTask periodicWrapper = new InstantPeriodicTask(decoratedRun, exec); |
| 134 |
Future<?> f; |
| 135 |
try { |
| 136 |
if (initialDelay <= 0L) { |
| 137 |
f = exec.submit(periodicWrapper); |
| 138 |
} else { |
| 139 |
f = exec.schedule(periodicWrapper, initialDelay, unit); |
| 140 |
} |
| 141 |
periodicWrapper.setFirst(f); |
| 142 |
} catch (RejectedExecutionException ex) { |
| 143 |
RxJavaPlugins.onError(ex); |
| 144 |
return EmptyDisposable.INSTANCE; |
| 145 |
} |
| 146 |
|
| 147 |
return periodicWrapper; |
| 148 |
} |
| 149 |
ScheduledDirectPeriodicTask task = new ScheduledDirectPeriodicTask(decoratedRun, true); |
| 150 |
try { |
| 151 |
Future<?> f = executor.get().scheduleAtFixedRate(task, initialDelay, period, unit); |
| 152 |
task.setFuture(f); |
| 153 |
return task; |
| 154 |
} catch (RejectedExecutionException ex) { |
| 155 |
RxJavaPlugins.onError(ex); |
| 156 |
return EmptyDisposable.INSTANCE; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
static final class ScheduledWorker extends Scheduler.Worker { |
| 161 |
|
| 162 |
final ScheduledExecutorService executor; |
| 163 |
|
| 164 |
final CompositeDisposable tasks; |
| 165 |
|
| 166 |
volatile boolean disposed; |
| 167 |
|
| 168 |
ScheduledWorker(ScheduledExecutorService executor) { |
| 169 |
this.executor = executor; |
| 170 |
this.tasks = new CompositeDisposable(); |
| 171 |
} |
| 172 |
|
| 173 |
@NonNull |
| 174 |
@Override |
| 175 |
public Disposable schedule(@NonNull Runnable run, long delay, @NonNull TimeUnit unit) { |
| 176 |
if (disposed) { |
| 177 |
return EmptyDisposable.INSTANCE; |
| 178 |
} |
| 179 |
|
| 180 |
Runnable decoratedRun = RxJavaPlugins.onSchedule(run); |
| 181 |
|
| 182 |
ScheduledRunnable sr = new ScheduledRunnable(decoratedRun, tasks); |
| 183 |
tasks.add(sr); |
| 184 |
|
| 185 |
try { |
| 186 |
Future<?> f; |
| 187 |
if (delay <= 0L) { |
| 188 |
f = executor.submit((Callable<Object>)sr); |
| 189 |
} else { |
| 190 |
f = executor.schedule((Callable<Object>)sr, delay, unit); |
| 191 |
} |
| 192 |
|
| 193 |
sr.setFuture(f); |
| 194 |
} catch (RejectedExecutionException ex) { |
| 195 |
dispose(); |
| 196 |
RxJavaPlugins.onError(ex); |
| 197 |
return EmptyDisposable.INSTANCE; |
| 198 |
} |
| 199 |
|
| 200 |
return sr; |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public void dispose() { |
| 205 |
if (!disposed) { |
| 206 |
disposed = true; |
| 207 |
tasks.dispose(); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
@Override |
| 212 |
public boolean isDisposed() { |
| 213 |
return disposed; |
| 214 |
} |
| 215 |
} |
| 216 |
} |