Repository navigation
Expand file tree
/
Copy pathExecutorScheduler.java
More file actions
195 lines (175 loc) · 6.7 KB
/
Copy pathExecutorScheduler.java
File metadata and controls
195 lines (175 loc) · 6.7 KB
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
/**
* Copyright 2014 Netflix, Inc.
*
* 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 rx.schedulers;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import rx.Scheduler;
import rx.Subscription;
import rx.functions.Action0;
import rx.plugins.RxJavaPlugins;
import rx.subscriptions.CompositeSubscription;
import rx.subscriptions.MultipleAssignmentSubscription;
import rx.subscriptions.Subscriptions;
/**
* Scheduler that wraps an Executor instance and establishes the Scheduler contract upon it.
* <p>
* Note that thread-hopping is unavoidable with this kind of Scheduler as we don't know about the underlying
* threading behavior of the executor.
*/
/* public */final class ExecutorScheduler extends Scheduler {
final Executor executor;
public ExecutorScheduler(Executor executor) {
this.executor = executor;
}
/**
* @warn javadoc missing
* @return
*/
@Override
public Worker createWorker() {
return new ExecutorSchedulerWorker(executor);
}
/** Worker that schedules tasks on the executor indirectly through a trampoline mechanism. */
static final class ExecutorSchedulerWorker extends Scheduler.Worker implements Runnable {
final Executor executor;
// TODO: use a better performing structure for task tracking
final CompositeSubscription tasks;
// TODO: use MpscLinkedQueue once available
final ConcurrentLinkedQueue<ExecutorAction> queue;
final AtomicInteger wip;
public ExecutorSchedulerWorker(Executor executor) {
this.executor = executor;
this.queue = new ConcurrentLinkedQueue<ExecutorAction>();
this.wip = new AtomicInteger();
this.tasks = new CompositeSubscription();
}
@Override
public Subscription schedule(Action0 action) {
if (isUnsubscribed()) {
return Subscriptions.unsubscribed();
}
ExecutorAction ea = new ExecutorAction(action, tasks);
tasks.add(ea);
queue.offer(ea);
if (wip.getAndIncrement() == 0) {
try {
executor.execute(this);
} catch (RejectedExecutionException t) {
// cleanup if rejected
tasks.remove(ea);
wip.decrementAndGet();
// report the error to the plugin
RxJavaPlugins.getInstance().getErrorHandler().handleError(t);
// throw it to the caller
throw t;
}
}
return ea;
}
@Override
public void run() {
do {
queue.poll().run();
} while (wip.decrementAndGet() > 0);
}
@Override
public Subscription schedule(final Action0 action, long delayTime, TimeUnit unit) {
if (delayTime <= 0) {
return schedule(action);
}
if (isUnsubscribed()) {
return Subscriptions.unsubscribed();
}
ScheduledExecutorService service;
if (executor instanceof ScheduledExecutorService) {
service = (ScheduledExecutorService)executor;
} else {
service = GenericScheduledExecutorService.getInstance();
}
final MultipleAssignmentSubscription mas = new MultipleAssignmentSubscription();
// tasks.add(mas); // Needs a removal without unsubscription
try {
Future<?> f = service.schedule(new Runnable() {
@Override
public void run() {
if (mas.isUnsubscribed()) {
return;
}
mas.set(schedule(action));
// tasks.delete(mas); // Needs a removal without unsubscription
}
}, delayTime, unit);
mas.set(Subscriptions.from(f));
} catch (RejectedExecutionException t) {
// report the rejection to plugins
RxJavaPlugins.getInstance().getErrorHandler().handleError(t);
throw t;
}
return mas;
}
@Override
public boolean isUnsubscribed() {
return tasks.isUnsubscribed();
}
@Override
public void unsubscribe() {
tasks.unsubscribe();
}
}
/** Runs the actual action and maintains an unsubscription state. */
static final class ExecutorAction implements Runnable, Subscription {
final Action0 actual;
final CompositeSubscription parent;
volatile int unsubscribed;
static final AtomicIntegerFieldUpdater<ExecutorAction> UNSUBSCRIBED_UPDATER
= AtomicIntegerFieldUpdater.newUpdater(ExecutorAction.class, "unsubscribed");
public ExecutorAction(Action0 actual, CompositeSubscription parent) {
this.actual = actual;
this.parent = parent;
}
@Override
public void run() {
if (isUnsubscribed()) {
return;
}
try {
actual.call();
} catch (Throwable t) {
RxJavaPlugins.getInstance().getErrorHandler().handleError(t);
Thread thread = Thread.currentThread();
thread.getUncaughtExceptionHandler().uncaughtException(thread, t);
} finally {
unsubscribe();
}
}
@Override
public boolean isUnsubscribed() {
return unsubscribed != 0;
}
@Override
public void unsubscribe() {
if (UNSUBSCRIBED_UPDATER.compareAndSet(this, 0, 1)) {
parent.remove(this);
}
}
}
}