25
25
import rx .plugins .RxJavaSchedulersHook ;
26
26
27
27
import java .util .concurrent .Executor ;
28
+ import java .util .concurrent .atomic .AtomicReference ;
28
29
29
30
/**
30
31
* Static factory methods for creating Schedulers.
@@ -35,13 +36,21 @@ public final class Schedulers {
35
36
private final Scheduler ioScheduler ;
36
37
private final Scheduler newThreadScheduler ;
37
38
38
- private static Schedulers INSTANCE ;
39
+ private static final AtomicReference < Schedulers > INSTANCE = new AtomicReference < Schedulers >() ;
39
40
40
- private static synchronized Schedulers getInstance () {
41
- if (INSTANCE == null ) {
42
- INSTANCE = new Schedulers ();
41
+ private static Schedulers getInstance () {
42
+ for (;;) {
43
+ Schedulers current = INSTANCE .get ();
44
+ if (current != null ) {
45
+ return current ;
46
+ }
47
+ current = new Schedulers ();
48
+ if (INSTANCE .compareAndSet (null , current )) {
49
+ return current ;
50
+ } else {
51
+ shutdown ();
52
+ }
43
53
}
44
- return INSTANCE ;
45
54
}
46
55
47
56
private Schedulers () {
@@ -163,7 +172,7 @@ public static Scheduler from(Executor executor) {
163
172
@ Experimental
164
173
public static void reset () {
165
174
shutdown ();
166
- INSTANCE = null ;
175
+ INSTANCE . set ( null ) ;
167
176
}
168
177
169
178
/**
@@ -205,11 +214,11 @@ public static void shutdown() {
205
214
if (s .newThreadScheduler instanceof SchedulerLifecycle ) {
206
215
((SchedulerLifecycle ) s .newThreadScheduler ).shutdown ();
207
216
}
208
-
217
+
209
218
GenericScheduledExecutorService .INSTANCE .shutdown ();
210
-
219
+
211
220
RxRingBuffer .SPSC_POOL .shutdown ();
212
-
221
+
213
222
RxRingBuffer .SPMC_POOL .shutdown ();
214
223
}
215
224
}
0 commit comments