|
60 | 60 | * interface which can grant access to the original or hooked {@code Runnable}, thus, a repeated {@code RxJavaPlugins.onSchedule}
|
61 | 61 | * can detect the earlier hook and not apply a new one over again.
|
62 | 62 | * <p>
|
63 |
| - * The default implementation of {@link #now(TimeUnit)} and {@link Worker#now(TimeUnit)} methods to return current |
64 |
| - * {@link System#currentTimeMillis()} value in the desired time unit. Custom {@code Scheduler} implementations can override this |
| 63 | + * The default implementation of {@link #now(TimeUnit)} and {@link Worker#now(TimeUnit)} methods to return current {@link System#currentTimeMillis()} |
| 64 | + * value in the desired time unit, unless {@code rx3.scheduler.use-nanotime} (boolean) is set. When the property is set to |
| 65 | + * {@code true}, the method uses {@link System#nanoTime()} as its basis instead. Custom {@code Scheduler} implementations can override this |
65 | 66 | * to provide specialized time accounting (such as virtual time to be advanced programmatically).
|
66 | 67 | * Note that operators requiring a {@code Scheduler} may rely on either of the {@code now()} calls provided by
|
67 | 68 | * {@code Scheduler} or {@code Worker} respectively, therefore, it is recommended they represent a logically
|
|
88 | 89 | * All methods on the {@code Scheduler} and {@code Worker} classes should be thread safe.
|
89 | 90 | */
|
90 | 91 | public abstract class Scheduler {
|
| 92 | + /** |
| 93 | + * Value representing whether to use {@link System#nanoTime()}, or default as clock for {@link #now(TimeUnit)} |
| 94 | + * and {@link Scheduler.Worker#now(TimeUnit)} |
| 95 | + * <p> |
| 96 | + * Associated system parameter: |
| 97 | + * <ul> |
| 98 | + * <li>{@code rx3.scheduler.use-nanotime}, boolean, default {@code false} |
| 99 | + * </ul> |
| 100 | + */ |
| 101 | + static boolean IS_DRIFT_USE_NANOTIME = Boolean.getBoolean("rx3.scheduler.use-nanotime"); |
| 102 | + |
| 103 | + /** |
| 104 | + * Returns the current clock time depending on state of {@link Scheduler#IS_DRIFT_USE_NANOTIME} in given {@code unit} |
| 105 | + * <p> |
| 106 | + * By default {@link System#currentTimeMillis()} will be used as the clock. When the property is set |
| 107 | + * {@link System#nanoTime()} will be used. |
| 108 | + * <p> |
| 109 | + * @param unit the time unit |
| 110 | + * @return the 'current time' in given unit |
| 111 | + * @throws NullPointerException if {@code unit} is {@code null} |
| 112 | + */ |
| 113 | + static long computeNow(TimeUnit unit) { |
| 114 | + if(!IS_DRIFT_USE_NANOTIME) { |
| 115 | + return unit.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS); |
| 116 | + } |
| 117 | + return unit.convert(System.nanoTime(), TimeUnit.NANOSECONDS); |
| 118 | + } |
| 119 | + |
91 | 120 | /**
|
92 | 121 | * The tolerance for a clock drift in nanoseconds where the periodic scheduler will rebase.
|
93 | 122 | * <p>
|
@@ -156,7 +185,7 @@ public static long clockDriftTolerance() {
|
156 | 185 | * @since 2.0
|
157 | 186 | */
|
158 | 187 | public long now(@NonNull TimeUnit unit) {
|
159 |
| - return unit.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS); |
| 188 | + return computeNow(unit); |
160 | 189 | }
|
161 | 190 |
|
162 | 191 | /**
|
@@ -362,8 +391,9 @@ public <S extends Scheduler & Disposable> S when(@NonNull Function<Flowable<Flow
|
362 | 391 | * track the individual {@code Runnable} tasks while they are waiting to be executed (with or without delay) so that
|
363 | 392 | * {@link #dispose()} can prevent their execution or potentially interrupt them if they are currently running.
|
364 | 393 | * <p>
|
365 |
| - * The default implementation of the {@link #now(TimeUnit)} method returns current |
366 |
| - * {@link System#currentTimeMillis()} value in the desired time unit. Custom {@code Worker} implementations can override this |
| 394 | + * The default implementation of the {@link #now(TimeUnit)} method returns current {@link System#currentTimeMillis()} |
| 395 | + * value in the desired time unit, unless {@code rx3.scheduler.use-nanotime} (boolean) is set. When the property is set to |
| 396 | + * {@code true}, the method uses {@link System#nanoTime()} as its basis instead. Custom {@code Worker} implementations can override this |
367 | 397 | * to provide specialized time accounting (such as virtual time to be advanced programmatically).
|
368 | 398 | * Note that operators requiring a scheduler may rely on either of the {@code now()} calls provided by
|
369 | 399 | * {@code Scheduler} or {@code Worker} respectively, therefore, it is recommended they represent a logically
|
@@ -482,7 +512,7 @@ public Disposable schedulePeriodically(@NonNull Runnable run, final long initial
|
482 | 512 | * @since 2.0
|
483 | 513 | */
|
484 | 514 | public long now(@NonNull TimeUnit unit) {
|
485 |
| - return unit.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS); |
| 515 | + return computeNow(unit); |
486 | 516 | }
|
487 | 517 |
|
488 | 518 | /**
|
|
0 commit comments