Top Description Inners Fields Constructors Methods
io.grpc

public final Class SynchronizationContext

extends Object
implements Executor
Class Inheritance
All Implemented Interfaces
java.util.concurrent.Executor
Annotations
@ThreadSafe
Static Imports
com.google.common.base.Preconditions.checkNotNull, .Preconditions.checkState

A synchronization context is a queue of tasks that run in sequence. It offers following guarantees:

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.

This class is thread-safe.

Since
1.17.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public static class
SynchronizationContext.ScheduledHandle

Allows the user to check the status and/or cancel a task scheduled by schedule.

Field Summary

Modifier and TypeField and Description
private final AtomicReference<Thread>
private final Queue<Runnable>
private final Thread.UncaughtExceptionHandler

Constructor Summary

AccessConstructor and Description
public
SynchronizationContext(Thread.UncaughtExceptionHandler
handles exceptions thrown out of the tasks. Different from what's documented on UncaughtExceptionHandler#uncaughtException, the thread is not terminated when the handler is called.
uncaughtExceptionHandler
)

Creates a SynchronizationContext.

Method Summary

Modifier and TypeMethod and Description
public final void
drain()

Run all tasks in the queue in the current thread, if no other thread is running this method.

public final void
execute(Runnable
the runnable task
task
)

Implements java.util.concurrent.Executor.execute.

Adds a task and run it in this synchronization context as soon as possible.
public final void
executeLater(Runnable runnable)

Adds a task that will be run when drain is called.

public final SynchronizationContext.ScheduledHandle

Returns:

an object for checking the status and/or cancel the scheduled task
schedule
(final Runnable
the task being scheduled
task
,
long
the delay
delay
,
TimeUnit
the time unit for the delay
unit
,
ScheduledExecutorService
the ScheduledExecutorService that provides delayed execution
timerService
)

Schedules a task to be added and run via execute after a delay.

public final SynchronizationContext.ScheduledHandle

Returns:

an object for checking the status and/or cancel the scheduled task
scheduleWithFixedDelay
(final Runnable
the task being scheduled
task
,
long
the delay before the first run
initialDelay
,
long
the delay after the first run.
delay
,
TimeUnit
the time unit for the delay
unit
,
ScheduledExecutorService
the ScheduledExecutorService that provides delayed execution
timerService
)

Schedules a task to be added and run via execute after an inital delay and then repeated after the delay until cancelled.

public void
throwIfNotInThisSynchronizationContext()

Throw IllegalStateException if this method is not called from this synchronization context.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

drainingThreadback to summary
private final AtomicReference<Thread> drainingThread
queueback to summary
private final Queue<Runnable> queue
uncaughtExceptionHandlerback to summary
private final Thread.UncaughtExceptionHandler uncaughtExceptionHandler

Constructor Detail

SynchronizationContextback to summary
public SynchronizationContext(Thread.UncaughtExceptionHandler uncaughtExceptionHandler)

Creates a SynchronizationContext.

Parameters
uncaughtExceptionHandler:Thread.UncaughtExceptionHandler

handles exceptions thrown out of the tasks. Different from what's documented on UncaughtExceptionHandler#uncaughtException, the thread is not terminated when the handler is called.

Method Detail

drainback to summary
public final void drain()

Run all tasks in the queue in the current thread, if no other thread is running this method. Otherwise do nothing.

Upon returning, it guarantees that all tasks submitted by #executeLater before it have been or will eventually be run, while not requiring any more calls to drain().

executeback to summary
public final void execute(Runnable task)

Implements java.util.concurrent.Executor.execute.

Adds a task and run it in this synchronization context as soon as possible. The task may run inline. If there are tasks that are previously queued by executeLater but have not been run, this method will trigger them to be run before the given task. This is equivalent to calling executeLater immediately followed by drain.

Parameters
task:Runnable

Doc from java.util.concurrent.Executor.execute.

the runnable task

Annotations
@Override
executeLaterback to summary
public final void executeLater(Runnable runnable)

Adds a task that will be run when drain is called.

This is useful for cases where you want to enqueue a task while under a lock of your own, but don't want the tasks to be run under your lock (for fear of deadlock). You can call executeLater in the lock, and call drain outside the lock.

scheduleback to summary
public final SynchronizationContext.ScheduledHandle schedule(final Runnable task, long delay, TimeUnit unit, ScheduledExecutorService timerService)

Schedules a task to be added and run via execute after a delay.

Parameters
task:Runnable

the task being scheduled

delay:long

the delay

unit:TimeUnit

the time unit for the delay

timerService:ScheduledExecutorService

the ScheduledExecutorService that provides delayed execution

Returns:SynchronizationContext.ScheduledHandle

an object for checking the status and/or cancel the scheduled task

scheduleWithFixedDelayback to summary
public final SynchronizationContext.ScheduledHandle scheduleWithFixedDelay(final Runnable task, long initialDelay, long delay, TimeUnit unit, ScheduledExecutorService timerService)

Schedules a task to be added and run via execute after an inital delay and then repeated after the delay until cancelled.

Parameters
task:Runnable

the task being scheduled

initialDelay:long

the delay before the first run

delay:long

the delay after the first run.

unit:TimeUnit

the time unit for the delay

timerService:ScheduledExecutorService

the ScheduledExecutorService that provides delayed execution

Returns:SynchronizationContext.ScheduledHandle

an object for checking the status and/or cancel the scheduled task

throwIfNotInThisSynchronizationContextback to summary
public void throwIfNotInThisSynchronizationContext()

Throw IllegalStateException if this method is not called from this synchronization context.

io.grpc back to summary

private Class SynchronizationContext.ManagedRunnable

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable

Field Summary

Modifier and TypeField and Description
pack-priv boolean
pack-priv boolean
pack-priv final Runnable

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

hasStartedback to summary
pack-priv boolean hasStarted
isCancelledback to summary
pack-priv boolean isCancelled
taskback to summary
pack-priv final Runnable task

Constructor Detail

ManagedRunnableback to summary
pack-priv ManagedRunnable(Runnable task)

Method Detail

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override
io.grpc back to summary

public final Class SynchronizationContext.ScheduledHandle

extends Object
Class Inheritance

Allows the user to check the status and/or cancel a task scheduled by schedule.

This class is NOT thread-safe. All methods must be run from the same SynchronizationContext as which the task was scheduled in.

Field Summary

Modifier and TypeField and Description
private final ScheduledFuture<?>
private final SynchronizationContext.ManagedRunnable

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
cancel()

Cancel the task if it's still pending.

public boolean
isPending()

Returns true if the task will eventually run, meaning that it has neither started running nor been cancelled.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

futureback to summary
private final ScheduledFuture<?> future
runnableback to summary
private final SynchronizationContext.ManagedRunnable runnable

Constructor Detail

ScheduledHandleback to summary
private ScheduledHandle(SynchronizationContext.ManagedRunnable runnable, ScheduledFuture<?> future)

Method Detail

cancelback to summary
public void cancel()

Cancel the task if it's still pending.

isPendingback to summary
public boolean isPending()

Returns true if the task will eventually run, meaning that it has neither started running nor been cancelled.