execute
and executeLater.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.
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| public static class | SynchronizationContext.ScheduledHandle
Allows the user to check the status and/or cancel a task scheduled by |
| Modifier and Type | Field and Description |
|---|---|
| private final AtomicReference | |
| private final Queue | |
| private final Thread. |
| Access | Constructor and Description |
|---|---|
| public | SynchronizationContext(Thread.
handles exceptions thrown out of the tasks. Different from
what's documented on uncaughtExceptionHandlerUncaughtExceptionHandler#uncaughtException, the thread is
not terminated when the handler is called.Creates a SynchronizationContext. |
| Modifier and Type | Method and Description |
|---|---|
| public final void | |
| public final void | execute(Runnable
the runnable task task)Implements java. |
| public final void | |
| public final SynchronizationContext. | Returns: an object for checking the status and/or cancel the scheduled taskthe task being scheduled task, long the delay delay, TimeUnit the time unit for the delay unit, ScheduledExecutorService the timerService)ScheduledExecutorService that provides delayed executionSchedules a task to be added and run via |
| public final SynchronizationContext. | Returns: an object for checking the status and/or cancel the scheduled taskthe 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 timerService)ScheduledExecutorService that provides delayed executionSchedules a task to be added and run via |
| public void | throwIfNotInThisSynchronizationContext()
Throw |
| drainingThread | back to summary |
|---|---|
| private final AtomicReference<Thread> drainingThread | |
| queue | back to summary |
|---|---|
| private final Queue<Runnable> queue | |
| uncaughtExceptionHandler | back to summary |
|---|---|
| private final Thread. | |
| SynchronizationContext | back to summary |
|---|---|
| public SynchronizationContext(Thread. Creates a SynchronizationContext.
| |
| drain | back 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 | |
| execute | back to summary |
|---|---|
| public final void execute(Runnable task) Implements java. 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 | back to summary |
|---|---|
| public final void executeLater(Runnable runnable) Adds a task that will be run when 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 | |
| schedule | back to summary |
|---|---|
| public final SynchronizationContext. Schedules a task to be added and run via
| |
| scheduleWithFixedDelay | back to summary |
|---|---|
| public final SynchronizationContext. Schedules a task to be added and run via
| |
| throwIfNotInThisSynchronizationContext | back to summary |
|---|---|
| public void throwIfNotInThisSynchronizationContext() Throw | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv boolean | |
| pack-priv boolean | |
| pack-priv final Runnable |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public void |
| hasStarted | back to summary |
|---|---|
| pack-priv boolean hasStarted | |
| isCancelled | back to summary |
|---|---|
| pack-priv boolean isCancelled | |
| task | back to summary |
|---|---|
| pack-priv final Runnable task | |
| ManagedRunnable | back to summary |
|---|---|
| pack-priv ManagedRunnable(Runnable task) | |
| run | back to summary |
|---|---|
| public void run() Implements java. Doc from java. Runs this operation.
| |
schedule.
This class is NOT thread-safe. All methods must be run from the same SynchronizationContext as which the task was scheduled in.
| Modifier and Type | Field and Description |
|---|---|
| private final ScheduledFuture | |
| private final SynchronizationContext. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public boolean | isPending()
Returns |
| future | back to summary |
|---|---|
| private final ScheduledFuture<?> future | |
| runnable | back to summary |
|---|---|
| private final SynchronizationContext. | |
| ScheduledHandle | back to summary |
|---|---|
| private ScheduledHandle(SynchronizationContext. | |
| cancel | back to summary |
|---|---|
| public void cancel() Cancel the task if it's still | |
| isPending | back to summary |
|---|---|
| public boolean isPending() Returns | |