@@ -48,7 +48,7 @@ private static Schedulers getInstance() {
4848 if (INSTANCE .compareAndSet (null , current )) {
4949 return current ;
5050 } else {
51- shutdown ();
51+ current . shutdownInstance ();
5252 }
5353 }
5454 }
@@ -168,8 +168,10 @@ public static Scheduler from(Executor executor) {
168168 */
169169 @ Experimental
170170 public static void reset () {
171- shutdown ();
172- INSTANCE .set (null );
171+ Schedulers s = INSTANCE .getAndSet (null );
172+ if (s != null ) {
173+ s .shutdownInstance ();
174+ }
173175 }
174176
175177 /**
@@ -178,16 +180,10 @@ public static void reset() {
178180 */
179181 /* public test only */ static void start () {
180182 Schedulers s = getInstance ();
183+
184+ s .startInstance ();
185+
181186 synchronized (s ) {
182- if (s .computationScheduler instanceof SchedulerLifecycle ) {
183- ((SchedulerLifecycle ) s .computationScheduler ).start ();
184- }
185- if (s .ioScheduler instanceof SchedulerLifecycle ) {
186- ((SchedulerLifecycle ) s .ioScheduler ).start ();
187- }
188- if (s .newThreadScheduler instanceof SchedulerLifecycle ) {
189- ((SchedulerLifecycle ) s .newThreadScheduler ).start ();
190- }
191187 GenericScheduledExecutorService .INSTANCE .start ();
192188
193189 RxRingBuffer .SPSC_POOL .start ();
@@ -201,22 +197,44 @@ public static void reset() {
201197 */
202198 public static void shutdown () {
203199 Schedulers s = getInstance ();
204- synchronized (s ) {
205- if (s .computationScheduler instanceof SchedulerLifecycle ) {
206- ((SchedulerLifecycle ) s .computationScheduler ).shutdown ();
207- }
208- if (s .ioScheduler instanceof SchedulerLifecycle ) {
209- ((SchedulerLifecycle ) s .ioScheduler ).shutdown ();
210- }
211- if (s .newThreadScheduler instanceof SchedulerLifecycle ) {
212- ((SchedulerLifecycle ) s .newThreadScheduler ).shutdown ();
213- }
200+ s .shutdownInstance ();
214201
202+ synchronized (s ) {
215203 GenericScheduledExecutorService .INSTANCE .shutdown ();
216-
204+
217205 RxRingBuffer .SPSC_POOL .shutdown ();
218206
219207 RxRingBuffer .SPMC_POOL .shutdown ();
220208 }
221209 }
210+
211+ /**
212+ * Start the instance-specific schedulers.
213+ */
214+ synchronized void startInstance () {
215+ if (computationScheduler instanceof SchedulerLifecycle ) {
216+ ((SchedulerLifecycle ) computationScheduler ).start ();
217+ }
218+ if (ioScheduler instanceof SchedulerLifecycle ) {
219+ ((SchedulerLifecycle ) ioScheduler ).start ();
220+ }
221+ if (newThreadScheduler instanceof SchedulerLifecycle ) {
222+ ((SchedulerLifecycle ) newThreadScheduler ).start ();
223+ }
224+ }
225+
226+ /**
227+ * Start the instance-specific schedulers.
228+ */
229+ synchronized void shutdownInstance () {
230+ if (computationScheduler instanceof SchedulerLifecycle ) {
231+ ((SchedulerLifecycle ) computationScheduler ).shutdown ();
232+ }
233+ if (ioScheduler instanceof SchedulerLifecycle ) {
234+ ((SchedulerLifecycle ) ioScheduler ).shutdown ();
235+ }
236+ if (newThreadScheduler instanceof SchedulerLifecycle ) {
237+ ((SchedulerLifecycle ) newThreadScheduler ).shutdown ();
238+ }
239+ }
222240}
0 commit comments