Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/schedulers/NewThreadWorker.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.schedulers;
15
16 import java.util.concurrent.*;
17
18 import io.reactivex.rxjava3.annotations.*;
19 import io.reactivex.rxjava3.core.Scheduler;
20 import io.reactivex.rxjava3.disposables.*;
21 import io.reactivex.rxjava3.internal.disposables.*;
22 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
23
24 /**
25 * Base class that manages a single-threaded ScheduledExecutorService as a
26 * worker but doesn't perform task-tracking operations.
27 *
28 */
29 public class NewThreadWorker extends Scheduler.Worker implements Disposable {
30 private final ScheduledExecutorService executor;
31
32 volatile boolean disposed;
33
34 public NewThreadWorker(ThreadFactory threadFactory) {
35 executor = SchedulerPoolFactory.create(threadFactory);
36 }
37
38 @NonNull
39 @Override
40 public Disposable schedule(@NonNull final Runnable run) {
41 return schedule(run, 0, null);
42 }
43
44 @NonNull
45 @Override
46 public Disposable schedule(@NonNull final Runnable action, long delayTime, @NonNull TimeUnit unit) {
47 if (disposed) {
48 return EmptyDisposable.INSTANCE;
49 }
50 return scheduleActual(action, delayTime, unit, null);
51 }
52
53 /**
54 * Schedules the given runnable on the underlying executor directly and
55 * returns its future wrapped into a Disposable.
56 * @param run the Runnable to execute in a delayed fashion
57 * @param delayTime the delay amount
58 * @param unit the delay time unit
59 * @return the ScheduledRunnable instance
60 */
61 public Disposable scheduleDirect(final Runnable run, long delayTime, TimeUnit unit) {
62 ScheduledDirectTask task = new ScheduledDirectTask(RxJavaPlugins.onSchedule(run), true);
63 try {
64 Future<?> f;
65 if (delayTime <= 0L) {
66 f = executor.submit(task);
67 } else {
68 f = executor.schedule(task, delayTime, unit);
69 }
70 task.setFuture(f);
71 return task;
72 } catch (RejectedExecutionException ex) {
73 RxJavaPlugins.onError(ex);
74 return EmptyDisposable.INSTANCE;
75 }
76 }
77
78 /**
79 * Schedules the given runnable periodically on the underlying executor directly
80 * and returns its future wrapped into a Disposable.
81 * @param run the Runnable to execute in a periodic fashion
82 * @param initialDelay the initial delay amount
83 * @param period the repeat period amount
84 * @param unit the time unit for both the initialDelay and period
85 * @return the ScheduledRunnable instance
86 */
87 public Disposable schedulePeriodicallyDirect(Runnable run, long initialDelay, long period, TimeUnit unit) {
88 final Runnable decoratedRun = RxJavaPlugins.onSchedule(run);
89 if (period <= 0L) {
90
91 InstantPeriodicTask periodicWrapper = new InstantPeriodicTask(decoratedRun, executor);
92 try {
93 Future<?> f;
94 if (initialDelay <= 0L) {
95 f = executor.submit(periodicWrapper);
96 } else {
97 f = executor.schedule(periodicWrapper, initialDelay, unit);
98 }
99 periodicWrapper.setFirst(f);
100 } catch (RejectedExecutionException ex) {
101 RxJavaPlugins.onError(ex);
102 return EmptyDisposable.INSTANCE;
103 }
104
105 return periodicWrapper;
106 }
107 ScheduledDirectPeriodicTask task = new ScheduledDirectPeriodicTask(decoratedRun, true);
108 try {
109 Future<?> f = executor.scheduleAtFixedRate(task, initialDelay, period, unit);
110 task.setFuture(f);
111 return task;
112 } catch (RejectedExecutionException ex) {
113 RxJavaPlugins.onError(ex);
114 return EmptyDisposable.INSTANCE;
115 }
116 }
117
118 /**
119 * Wraps and returns the given runnable into a ScheduledRunnable and schedules it
120 * on the underlying ScheduledExecutorService.
121 * @param run the runnable instance
122 * @param delayTime the time to delay the execution
123 * @param unit the time unit
124 * @param parent the optional tracker parent to add the created ScheduledRunnable instance to before it gets scheduled
125 * @return the ScheduledRunnable instance
126 */
127 @NonNull
128 public ScheduledRunnable scheduleActual(final Runnable run, long delayTime, @NonNull TimeUnit unit, @Nullable DisposableContainer parent) {
129 Runnable decoratedRun = RxJavaPlugins.onSchedule(run);
130
131 ScheduledRunnable sr = new ScheduledRunnable(decoratedRun, parent);
132
133 if (parent != null) {
134 if (!parent.add(sr)) {
135 return sr;
136 }
137 }
138
139 Future<?> f;
140 try {
141 if (delayTime <= 0) {
142 f = executor.submit((Callable<Object>)sr);
143 } else {
144 f = executor.schedule((Callable<Object>)sr, delayTime, unit);
145 }
146 sr.setFuture(f);
147 } catch (RejectedExecutionException ex) {
148 if (parent != null) {
149 parent.remove(sr);
150 }
151 RxJavaPlugins.onError(ex);
152 }
153
154 return sr;
155 }
156
157 @Override
158 public void dispose() {
159 if (!disposed) {
160 disposed = true;
161 executor.shutdownNow();
162 }
163 }
164
165 /**
166 * Shuts down the underlying executor in a non-interrupting fashion.
167 */
168 public void shutdown() {
169 if (!disposed) {
170 disposed = true;
171 executor.shutdown();
172 }
173 }
174
175 @Override
176 public boolean isDisposed() {
177 return disposed;
178 }
179 }