-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
ExecutorScheduler.java
355 lines (291 loc) · 10.6 KB
/
ExecutorScheduler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/**
* 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.internal.schedulers;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import io.reactivex.Scheduler;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.*;
import io.reactivex.internal.disposables.*;
import io.reactivex.internal.functions.Functions;
import io.reactivex.internal.queue.MpscLinkedQueue;
import io.reactivex.internal.schedulers.ExecutorScheduler.ExecutorWorker.BooleanRunnable;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.schedulers.*;
/**
* Wraps an Executor and provides the Scheduler API over it.
*/
public final class ExecutorScheduler extends Scheduler {
@NonNull
final Executor executor;
static final Scheduler HELPER = Schedulers.single();
public ExecutorScheduler(@NonNull Executor executor) {
this.executor = executor;
}
@NonNull
@Override
public Worker createWorker() {
return new ExecutorWorker(executor);
}
@NonNull
@Override
public Disposable scheduleDirect(@NonNull Runnable run) {
Runnable decoratedRun = RxJavaPlugins.onSchedule(run);
try {
if (executor instanceof ExecutorService) {
ScheduledDirectTask task = new ScheduledDirectTask(decoratedRun);
Future<?> f = ((ExecutorService)executor).submit(task);
task.setFuture(f);
return task;
}
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);
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 = 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);
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 Executor executor;
final MpscLinkedQueue<Runnable> queue;
volatile boolean disposed;
final AtomicInteger wip = new AtomicInteger();
final CompositeDisposable tasks = new CompositeDisposable();
public ExecutorWorker(Executor executor) {
this.executor = executor;
this.queue = new MpscLinkedQueue<Runnable>();
}
@NonNull
@Override
public Disposable schedule(@NonNull Runnable run) {
if (disposed) {
return EmptyDisposable.INSTANCE;
}
Runnable decoratedRun = RxJavaPlugins.onSchedule(run);
BooleanRunnable br = new BooleanRunnable(decoratedRun);
queue.offer(br);
if (wip.getAndIncrement() == 0) {
try {
executor.execute(this);
} catch (RejectedExecutionException ex) {
disposed = true;
queue.clear();
RxJavaPlugins.onError(ex);
return EmptyDisposable.INSTANCE;
}
}
return br;
}
@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<Object>)sr, delay, unit);
sr.setFuture(f);
} catch (RejectedExecutionException ex) {
disposed = true;
RxJavaPlugins.onError(ex);
return EmptyDisposable.INSTANCE;
}
} else {
final Disposable d = HELPER.scheduleDirect(sr, delay, unit);
sr.setFuture(new DisposeOnCancel(d));
}
first.replace(sr);
return mar;
}
@Override
public void dispose() {
if (!disposed) {
disposed = true;
tasks.dispose();
if (wip.getAndIncrement() == 0) {
queue.clear();
}
}
}
@Override
public boolean isDisposed() {
return disposed;
}
@Override
public void run() {
int missed = 1;
final MpscLinkedQueue<Runnable> q = queue;
for (;;) {
if (disposed) {
q.clear();
return;
}
for (;;) {
Runnable run = q.poll();
if (run == null) {
break;
}
run.run();
if (disposed) {
q.clear();
return;
}
}
if (disposed) {
q.clear();
return;
}
missed = wip.addAndGet(-missed);
if (missed == 0) {
break;
}
}
}
static final class BooleanRunnable extends AtomicBoolean implements Runnable, Disposable {
private static final long serialVersionUID = -2421395018820541164L;
final Runnable actual;
BooleanRunnable(Runnable actual) {
this.actual = actual;
}
@Override
public void run() {
if (get()) {
return;
}
try {
actual.run();
} finally {
lazySet(true);
}
}
@Override
public void dispose() {
lazySet(true);
}
@Override
public boolean isDisposed() {
return get();
}
}
final class SequentialDispose implements Runnable {
private final SequentialDisposable mar;
private final Runnable decoratedRun;
SequentialDispose(SequentialDisposable mar, Runnable decoratedRun) {
this.mar = mar;
this.decoratedRun = decoratedRun;
}
@Override
public void run() {
mar.replace(schedule(decoratedRun));
}
}
}
static final class DelayedRunnable extends AtomicReference<Runnable>
implements Runnable, Disposable, SchedulerRunnableIntrospection {
private static final long serialVersionUID = -4101336210206799084L;
final SequentialDisposable timed;
final SequentialDisposable direct;
DelayedRunnable(Runnable run) {
super(run);
this.timed = new SequentialDisposable();
this.direct = new SequentialDisposable();
}
@Override
public void run() {
Runnable r = get();
if (r != null) {
try {
r.run();
} finally {
lazySet(null);
timed.lazySet(DisposableHelper.DISPOSED);
direct.lazySet(DisposableHelper.DISPOSED);
}
}
}
@Override
public boolean isDisposed() {
return get() == null;
}
@Override
public void dispose() {
if (getAndSet(null) != null) {
timed.dispose();
direct.dispose();
}
}
@Override
public Runnable getWrappedRunnable() {
Runnable r = get();
return r != null ? r : Functions.EMPTY_RUNNABLE;
}
}
final class DelayedDispose implements Runnable {
private final DelayedRunnable dr;
DelayedDispose(DelayedRunnable dr) {
this.dr = dr;
}
@Override
public void run() {
dr.direct.replace(scheduleDirect(dr));
}
}
}