Skip to content

Instantly share code, notes, and snippets.

@SergejIsbrecht
Created January 25, 2021 21:49
Show Gist options
  • Save SergejIsbrecht/45537fba998905376a2db42b3a1cc04f to your computer and use it in GitHub Desktop.
Save SergejIsbrecht/45537fba998905376a2db42b3a1cc04f to your computer and use it in GitHub Desktop.
State of the RxJava Scheduler + Time
Scheduler {
Default:
Disposable scheduleDirect(@NonNull Runnable run)
Disposable scheduleDirect(@NonNull Runnable run, long delay, @NonNull TimeUnit unit) // depends on Worker default-impl
Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, @NonNull TimeUnit unit) // uses now(TimeUnit) transitively from Worker default-impl
long now(@NonNull TimeUnit unit)
Abstract:
abstract Worker createWorker()
}
Scheduler.Worker {
Default:
Disposable schedule(@NonNull Runnable run)
Disposable schedulePeriodically(@NonNull Runnable run, final long initialDelay, final long period, @NonNull final TimeUnit unit) // uses now(TimeUnit.NANOSECONDS)
long now(@NonNull TimeUnit unit)
Abstract:
abstract Disposable schedule(@NonNull Runnable run, long delay, @NonNull TimeUnit unit)
}
---
SingleScheduler {
Override:
Disposable scheduleDirect(@NonNull Runnable run, long delay, TimeUnit unit)
Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, TimeUnit unit)
Worker createWorker()
}
SingleScheduler.Worker {
Override:
Disposable schedule(@NonNull Runnable run, long delay, @NonNull TimeUnit unit)
}
Problem:
`SingleScheduler` uses default impl `schedulePeriodically` from `SingleScheduler.Worker`, but overwrites `Scheduler#scheduleDirect` (with delay) & `Scheduler#schedulePeriodicallyDirect`
=> `SingleScheduler` uses a `ScheduledExecutorService` internally
=> `ScheduledExecutorService` uses `System.nanoTime` and `SingleScheduler.Worker#schedulePeriodically` uses `System.currentTimeMillis`
`Scheduler.Worker#schedulePeriodically`
* FlowableInterval
* FlowableBufferTimed
* FlowableWindowTimed
`Scheduler#schedulePeriodicallyDirect`
* FlowableInterval
* FlowableSampleTimed
* FlowableWindowTimed
`Scheduler#scheduleDirect` (with delay)
* ObservableTimer
* SingleTimeout
`Scheduler.Worker#schedule` (with delay)
* FlowableDebounceTimed
* FlowableDelay
* FlowableThrottleLatest
ExecutorScheduler {
@Override
Disposable scheduleDirect(@NonNull Runnable run)
Disposable scheduleDirect(@NonNull Runnable run, final long delay, final TimeUnit unit)
Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, TimeUnit unit) {
// when ScheduledExecutorService -> use ScheduledExecutorService to schedule periodically (ScheduledExecutorService#scheduleAtFixedRate)
// else use super impl
}
}
ExecutorScheduler.ExecutorWorker {
@Override
Disposable schedule(@NonNull Runnable run)
Disposable schedule(@NonNull Runnable run, long delay, @NonNull TimeUnit unit) {
// when ScheduledExecutorService -> use ScheduledExecutorService to schedule periodically
// else -> use Single to schedule with delay
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment