Class/Object

monix.execution.schedulers

TrampolineScheduler

Related Docs: object TrampolineScheduler | package schedulers

Permalink

final class TrampolineScheduler extends Scheduler

A Scheduler implementation that executes runnables immediately, on the current thread, by means of a trampoline implementation.

Can be used in some cases to keep the asynchronous execution on the current thread, as an optimization, but be warned, you have to know what you're doing.

The TrampolineScheduler keeps a reference to another underlying scheduler, to which it defers for:

Deferring the rest of the queue happens:

Thus this implementation is compatible with the scala.concurrent.BlockContext, detecting blocking blocks and reacting by forking the rest of the queue to prevent deadlocks.

Self Type
TrampolineScheduler
Linear Supertypes
Scheduler, Executor, UncaughtExceptionReporter, Serializable, Serializable, ExecutionContext, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. TrampolineScheduler
  2. Scheduler
  3. Executor
  4. UncaughtExceptionReporter
  5. Serializable
  6. Serializable
  7. ExecutionContext
  8. AnyRef
  9. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new TrampolineScheduler(underlying: Scheduler, executionModel: ExecutionModel)

    Permalink

    underlying

    is the ExecutionContext to which the it defers to in case real asynchronous is needed

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clockMonotonic(unit: TimeUnit): Long

    Permalink

    Returns a monotonic clock measurement, if supported by the underlying platform.

    Returns a monotonic clock measurement, if supported by the underlying platform.

    This is the pure equivalent of Java's System.nanoTime, or of CLOCK_MONOTONIC from Linux's clock_gettime().

    timer.clockMonotonic(NANOSECONDS)

    The returned value can have nanoseconds resolution and represents the number of time units elapsed since some fixed but arbitrary origin time. Usually this is the Unix epoch, but that's not a guarantee, as due to the limits of Long this will overflow in the future (263 is about 292 years in nanoseconds) and the implementation reserves the right to change the origin.

    The return value should not be considered related to wall-clock time, the primary use-case being to take time measurements and compute differences between such values, for example in order to measure the time it took to execute a task.

    As a matter of implementation detail, Monix's Scheduler implementations use System.nanoTime and the JVM will use CLOCK_MONOTONIC when available, instead of CLOCK_REALTIME (see clock_gettime() on Linux) and it is up to the underlying platform to implement it correctly.

    And be warned, there are platforms that don't have a correct implementation of CLOCK_MONOTONIC. For example at the moment of writing there is no standard way for such a clock on top of JavaScript and the situation isn't so clear cut for the JVM either, see:

    The JVM tries to do the right thing and at worst the resolution and behavior will be that of System.currentTimeMillis.

    The recommendation is to use this monotonic clock when doing measurements of execution time, or if you value monotonically increasing values more than a correspondence to wall-time, or otherwise prefer clockRealTime.

    Definition Classes
    TrampolineSchedulerScheduler
  6. def clockRealTime(unit: TimeUnit): Long

    Permalink

    Returns the current time, as a Unix timestamp (number of time units since the Unix epoch).

    Returns the current time, as a Unix timestamp (number of time units since the Unix epoch).

    This is the equivalent of Java's System.currentTimeMillis, or of CLOCK_REALTIME from Linux's clock_gettime().

    The provided TimeUnit determines the time unit of the output, its precision, but not necessarily its resolution, which is implementation dependent. For example this will return the number of milliseconds since the epoch:

    import scala.concurrent.duration.MILLISECONDS
    
    scheduler.clockRealTime(MILLISECONDS)

    N.B. the resolution is limited by the underlying implementation and by the underlying CPU and OS. If the implementation uses System.currentTimeMillis, then it can't have a better resolution than 1 millisecond, plus depending on underlying runtime (e.g. Node.js) it might return multiples of 10 milliseconds or more.

    See clockMonotonic, for fetching a monotonic value that may be better suited for doing time measurements.

    Definition Classes
    TrampolineSchedulerScheduler
  7. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  10. def execute(runnable: Runnable): Unit

    Permalink

    Schedules the given command for execution at some time in the future.

    Schedules the given command for execution at some time in the future.

    The command may execute in a new thread, in a pooled thread, in the calling thread, basically at the discretion of the Scheduler implementation.

    Definition Classes
    TrampolineSchedulerScheduler → Executor → ExecutionContext
  11. val executionModel: ExecutionModel

    Permalink

    The ExecutionModel is a specification of how run-loops and producers should behave in regards to executing tasks either synchronously or asynchronously.

    The ExecutionModel is a specification of how run-loops and producers should behave in regards to executing tasks either synchronously or asynchronously.

    Definition Classes
    TrampolineSchedulerScheduler
  12. def features: Features

    Permalink

    Exposes a set of flags that describes the Scheduler's features.

    Exposes a set of flags that describes the Scheduler's features.

    Definition Classes
    TrampolineSchedulerScheduler
  13. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  14. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  15. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  16. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  17. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  18. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  19. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  20. def prepare(): ExecutionContext

    Permalink
    Definition Classes
    ExecutionContext
  21. def reportFailure(t: Throwable): Unit

    Permalink

    Reports that an asynchronous computation failed.

    Reports that an asynchronous computation failed.

    Definition Classes
    TrampolineSchedulerSchedulerUncaughtExceptionReporter → ExecutionContext
  22. def scheduleAtFixedRate(initialDelay: Long, period: Long, unit: TimeUnit, r: Runnable): Cancelable

    Permalink

    Schedules a periodic task that becomes enabled first after the given initial delay, and subsequently with the given period.

    Schedules a periodic task that becomes enabled first after the given initial delay, and subsequently with the given period. Executions will commence after initialDelay then initialDelay + period, then initialDelay + 2 * period and so on.

    If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the scheduler. If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.

    For example the following schedules a message to be printed to standard output approximately every 10 seconds with an initial delay of 5 seconds:

    val task = scheduler.scheduleAtFixedRate(5, 10, TimeUnit.SECONDS, new Runnable {
      def run() = print("Repeated message")
    })
    
    // later if you change your mind ...
    task.cancel()
    initialDelay

    is the time to wait until the first execution happens

    period

    is the time to wait between 2 successive executions of the task

    unit

    is the time unit used for the initialDelay and the period parameters

    r

    is the callback to be executed

    returns

    a cancelable that can be used to cancel the execution of this repeated task at any time.

    Definition Classes
    TrampolineSchedulerScheduler
  23. def scheduleOnce(initialDelay: Long, unit: TimeUnit, r: Runnable): Cancelable

    Permalink

    Schedules a task to run in the future, after initialDelay.

    Schedules a task to run in the future, after initialDelay.

    For example the following schedules a message to be printed to standard output after 5 minutes:

    val task = scheduler.scheduleOnce(5, TimeUnit.MINUTES, new Runnable {
      def run() = print("Hello, world!")
    })
    
    // later if you change your mind ...
    task.cancel()
    initialDelay

    is the time to wait until the execution happens

    unit

    is the time unit used for initialDelay

    r

    is the callback to be executed

    returns

    a Cancelable that can be used to cancel the created task before execution.

    Definition Classes
    TrampolineSchedulerScheduler
  24. def scheduleWithFixedDelay(initialDelay: Long, delay: Long, unit: TimeUnit, r: Runnable): Cancelable

    Permalink

    Schedules for execution a periodic task that is first executed after the given initial delay and subsequently with the given delay between the termination of one execution and the commencement of the next.

    Schedules for execution a periodic task that is first executed after the given initial delay and subsequently with the given delay between the termination of one execution and the commencement of the next.

    For example the following schedules a message to be printed to standard output every 10 seconds with an initial delay of 5 seconds:

    val task = s.scheduleWithFixedDelay(5, 10, TimeUnit.SECONDS, new Runnable {
      def run() = print("Repeated message")
    })
    
    // later if you change your mind ...
    task.cancel()
    initialDelay

    is the time to wait until the first execution happens

    delay

    is the time to wait between 2 successive executions of the task

    unit

    is the time unit used for the initialDelay and the delay parameters

    r

    is the callback to be executed

    returns

    a cancelable that can be used to cancel the execution of this repeated task at any time.

    Definition Classes
    TrampolineSchedulerScheduler
  25. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  26. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  27. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  28. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  29. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  30. def withExecutionModel(em: ExecutionModel): TrampolineScheduler

    Permalink

    Given a function that will receive the underlying ExecutionModel, returns a new Scheduler reference, based on the source, that exposes the transformed ExecutionModel when queried by means of the executionModel property.

    Given a function that will receive the underlying ExecutionModel, returns a new Scheduler reference, based on the source, that exposes the transformed ExecutionModel when queried by means of the executionModel property.

    This method enables reusing global scheduler references in a local scope, but with a slightly modified execution model to inject.

    The contract of this method (things you can rely on):

    1. the source Scheduler must not be modified in any way
    2. the implementation should wrap the source efficiently, such that the result mirrors the source Scheduler in every way except for the execution model

    Sample:

    import monix.execution.Scheduler.global
    
    implicit val scheduler = {
      val em = global.executionModel
      global.withExecutionModel(em.withAutoCancelableLoops(true))
    }
    Definition Classes
    TrampolineSchedulerScheduler
  31. def withUncaughtExceptionReporter(r: UncaughtExceptionReporter): TrampolineScheduler

    Permalink

    Returns a new Scheduler that piggybacks on this, but uses the given exception reporter for reporting uncaught errors.

    Returns a new Scheduler that piggybacks on this, but uses the given exception reporter for reporting uncaught errors.

    Sample:

    import monix.execution.Scheduler.global
    import monix.execution.UncaughtExceptionReporter
    
    implicit val scheduler = {
      val r = UncaughtExceptionReporter { e =>
        e.printStackTrace()
      }
      global.withUncaughtExceptionReporter(r)
    }
    Definition Classes
    TrampolineSchedulerScheduler

Inherited from Scheduler

Inherited from Executor

Inherited from UncaughtExceptionReporter

Inherited from Serializable

Inherited from Serializable

Inherited from ExecutionContext

Inherited from AnyRef

Inherited from Any

Ungrouped