16
16
package rx .schedulers ;
17
17
18
18
import rx .Scheduler ;
19
- import rx .internal .schedulers .*;
19
+ import rx .annotations .Experimental ;
20
+ import rx .internal .schedulers .ExecutorScheduler ;
21
+ import rx .internal .schedulers .GenericScheduledExecutorService ;
22
+ import rx .internal .schedulers .SchedulerLifecycle ;
20
23
import rx .internal .util .RxRingBuffer ;
21
24
import rx .plugins .RxJavaPlugins ;
22
25
import rx .plugins .RxJavaSchedulersHook ;
@@ -32,7 +35,14 @@ public final class Schedulers {
32
35
private final Scheduler ioScheduler ;
33
36
private final Scheduler newThreadScheduler ;
34
37
35
- private static final Schedulers INSTANCE = new Schedulers ();
38
+ private static Schedulers INSTANCE ;
39
+
40
+ private static synchronized Schedulers getInstance () {
41
+ if (INSTANCE == null ) {
42
+ INSTANCE = new Schedulers ();
43
+ }
44
+ return INSTANCE ;
45
+ }
36
46
37
47
private Schedulers () {
38
48
RxJavaSchedulersHook hook = RxJavaPlugins .getInstance ().getSchedulersHook ();
@@ -86,7 +96,7 @@ public static Scheduler trampoline() {
86
96
* @return a {@link Scheduler} that creates new threads
87
97
*/
88
98
public static Scheduler newThread () {
89
- return INSTANCE .newThreadScheduler ;
99
+ return getInstance () .newThreadScheduler ;
90
100
}
91
101
92
102
/**
@@ -101,7 +111,7 @@ public static Scheduler newThread() {
101
111
* @return a {@link Scheduler} meant for computation-bound work
102
112
*/
103
113
public static Scheduler computation () {
104
- return INSTANCE .computationScheduler ;
114
+ return getInstance () .computationScheduler ;
105
115
}
106
116
107
117
/**
@@ -118,7 +128,7 @@ public static Scheduler computation() {
118
128
* @return a {@link Scheduler} meant for IO-bound work
119
129
*/
120
130
public static Scheduler io () {
121
- return INSTANCE .ioScheduler ;
131
+ return getInstance () .ioScheduler ;
122
132
}
123
133
124
134
/**
@@ -141,13 +151,27 @@ public static TestScheduler test() {
141
151
public static Scheduler from (Executor executor ) {
142
152
return new ExecutorScheduler (executor );
143
153
}
154
+
155
+ /**
156
+ * Resets the current {@link Schedulers} instance.
157
+ * <p>
158
+ * This API is experimental. Resetting the schedulers is dangerous
159
+ * during application runtime and also bad code could invoke it in
160
+ * the middle of an application life-cycle and really break applications
161
+ * if not used cautiously.
162
+ */
163
+ @ Experimental
164
+ public static void reset () {
165
+ shutdown ();
166
+ INSTANCE = null ;
167
+ }
144
168
145
169
/**
146
170
* Starts those standard Schedulers which support the SchedulerLifecycle interface.
147
171
* <p>The operation is idempotent and threadsafe.
148
172
*/
149
173
/* public test only */ static void start () {
150
- Schedulers s = INSTANCE ;
174
+ Schedulers s = getInstance () ;
151
175
synchronized (s ) {
152
176
if (s .computationScheduler instanceof SchedulerLifecycle ) {
153
177
((SchedulerLifecycle ) s .computationScheduler ).start ();
@@ -170,7 +194,7 @@ public static Scheduler from(Executor executor) {
170
194
* <p>The operation is idempotent and threadsafe.
171
195
*/
172
196
public static void shutdown () {
173
- Schedulers s = INSTANCE ;
197
+ Schedulers s = getInstance () ;
174
198
synchronized (s ) {
175
199
if (s .computationScheduler instanceof SchedulerLifecycle ) {
176
200
((SchedulerLifecycle ) s .computationScheduler ).shutdown ();
@@ -189,4 +213,4 @@ public static void shutdown() {
189
213
RxRingBuffer .SPMC_POOL .shutdown ();
190
214
}
191
215
}
192
- }
216
+ }
0 commit comments