Package io.grpc

Class SynchronizationContext

  • All Implemented Interfaces:
    Executor

    @ThreadSafe
    public final class SynchronizationContext
    extends Object
    implements Executor
    A synchronization context is a queue of tasks that run in sequence. It offers following guarantees:
    • Ordering. Tasks are run in the same order as they are submitted via execute(java.lang.Runnable) and executeLater(java.lang.Runnable).
    • Serialization. Tasks are run in sequence and establish a happens-before relationship between them.
    • Non-reentrancy. If a task running in a synchronization context executes or schedules another task in the same synchronization context, the latter task will never run inline. It will instead be queued and run only after the current task has returned.

    It doesn't own any thread. Tasks are run from caller's or caller-provided threads.

    Conceptually, it is fairly accurate to think of SynchronizationContext like a cheaper Executors.newSingleThreadExecutor() when used for synchronization (not long-running tasks). Both use a queue for tasks that are run in order and neither guarantee that tasks have completed before returning from execute(). However, the behavior does diverge if locks are held when calling the context. So it is encouraged to avoid mixing locks and synchronization context except via executeLater(java.lang.Runnable).

    This class is thread-safe.

    Since:
    1.17.0