|
61 | 61 | * interface which can grant access to the original or hooked {@code Runnable}, thus, a repeated {@code RxJavaPlugins.onSchedule}
|
62 | 62 | * can detect the earlier hook and not apply a new one over again.
|
63 | 63 | * <p>
|
64 |
| - * The default implementation of {@link #now(TimeUnit)} and {@link Worker#now(TimeUnit)} methods to return current |
65 |
| - * {@link System#currentTimeMillis()} value in the desired time unit. Custom {@code Scheduler} implementations can override this |
| 64 | + * The default implementation of {@link #now(TimeUnit)} and {@link Worker#now(TimeUnit)} methods to return current {@link System#currentTimeMillis()} |
| 65 | + * value in the desired time unit, unless {@code rx2.scheduler.use-nanotime} (boolean) is set. When the property is set to |
| 66 | + * {@code true}, the method uses {@link System#nanoTime()} as its basis instead. Custom {@code Scheduler} implementations can override this |
66 | 67 | * to provide specialized time accounting (such as virtual time to be advanced programmatically).
|
67 | 68 | * Note that operators requiring a {@code Scheduler} may rely on either of the {@code now()} calls provided by
|
68 | 69 | * {@code Scheduler} or {@code Worker} respectively, therefore, it is recommended they represent a logically
|
|
89 | 90 | * All methods on the {@code Scheduler} and {@code Worker} classes should be thread safe.
|
90 | 91 | */
|
91 | 92 | public abstract class Scheduler {
|
| 93 | + /** |
| 94 | + * Value representing whether to use {@link System#nanoTime()}, or default as clock for {@link #now(TimeUnit)} |
| 95 | + * and {@link Scheduler.Worker#now(TimeUnit)} |
| 96 | + * <p> |
| 97 | + * Associated system parameter: |
| 98 | + * <ul> |
| 99 | + * <li>{@code rx2.scheduler.use-nanotime}, boolean, default {@code false} |
| 100 | + * </ul> |
| 101 | + */ |
| 102 | + static boolean IS_DRIFT_USE_NANOTIME = Boolean.getBoolean("rx2.scheduler.use-nanotime"); |
| 103 | + |
| 104 | + /** |
| 105 | + * Returns the current clock time depending on state of {@link Scheduler#IS_DRIFT_USE_NANOTIME} in given {@code unit} |
| 106 | + * <p> |
| 107 | + * By default {@link System#currentTimeMillis()} will be used as the clock. When the property is set |
| 108 | + * {@link System#nanoTime()} will be used. |
| 109 | + * <p> |
| 110 | + * @param unit the time unit |
| 111 | + * @return the 'current time' in given unit |
| 112 | + * @throws NullPointerException if {@code unit} is {@code null} |
| 113 | + */ |
| 114 | + static long computeNow(TimeUnit unit) { |
| 115 | + if(!IS_DRIFT_USE_NANOTIME) { |
| 116 | + return unit.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS); |
| 117 | + } |
| 118 | + return unit.convert(System.nanoTime(), TimeUnit.NANOSECONDS); |
| 119 | + } |
| 120 | + |
92 | 121 | /**
|
93 | 122 | * The tolerance for a clock drift in nanoseconds where the periodic scheduler will rebase.
|
94 | 123 | * <p>
|
@@ -131,7 +160,7 @@ public static long clockDriftTolerance() {
|
131 | 160 | * @since 2.0
|
132 | 161 | */
|
133 | 162 | public long now(@NonNull TimeUnit unit) {
|
134 |
| - return unit.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS); |
| 163 | + return computeNow(unit); |
135 | 164 | }
|
136 | 165 |
|
137 | 166 | /**
|
@@ -332,8 +361,9 @@ public <S extends Scheduler & Disposable> S when(@NonNull Function<Flowable<Flow
|
332 | 361 | * track the individual {@code Runnable} tasks while they are waiting to be executed (with or without delay) so that
|
333 | 362 | * {@link #dispose()} can prevent their execution or potentially interrupt them if they are currently running.
|
334 | 363 | * <p>
|
335 |
| - * The default implementation of the {@link #now(TimeUnit)} method returns current |
336 |
| - * {@link System#currentTimeMillis()} value in the desired time unit. Custom {@code Worker} implementations can override this |
| 364 | + * The default implementation of the {@link #now(TimeUnit)} method returns current {@link System#currentTimeMillis()} |
| 365 | + * value in the desired time unit, unless {@code rx2.scheduler.use-nanotime} (boolean) is set. When the property is set to |
| 366 | + * {@code true}, the method uses {@link System#nanoTime()} as its basis instead. Custom {@code Worker} implementations can override this |
337 | 367 | * to provide specialized time accounting (such as virtual time to be advanced programmatically).
|
338 | 368 | * Note that operators requiring a scheduler may rely on either of the {@code now()} calls provided by
|
339 | 369 | * {@code Scheduler} or {@code Worker} respectively, therefore, it is recommended they represent a logically
|
@@ -448,7 +478,7 @@ public Disposable schedulePeriodically(@NonNull Runnable run, final long initial
|
448 | 478 | * @since 2.0
|
449 | 479 | */
|
450 | 480 | public long now(@NonNull TimeUnit unit) {
|
451 |
| - return unit.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS); |
| 481 | + return computeNow(unit); |
452 | 482 | }
|
453 | 483 |
|
454 | 484 | /**
|
|
0 commit comments