public final class Synchronizer extends Object implements SynchronizerApi.SleepRunGet
Synchronizer, the calling code defines a chain of actions that is
completed with invoke(). All those actions are performed in one synchronized block using a lock which is
private to the Synchronizer instance.synchronized blocks
and Object.wait() / Object.notifyAll() directly.Synchronizer provides the following actions:true
sleepUntil) and/or after (thenSleepUntil) the main action.Object.wait() is called inside
a loop to ensure liveness and safety (see
SEI
CERT rule THI03-J for details).UncheckedInterruptedException is thrown and the
thread's interrupted flag is re-set.wakeOthers() is equivalent to calling Object.notifyAll(). If the main action is a
Supplier<T>, the calling code can decide whether waking takes place by using
andWakeOthersIf(Predicate<T>) instead. synchronizer.run(...)
.thenSleepUntil(...)
.invoke();
synchronizer.sleepUntil(...)
.run(...)
.andWakeOthers()
.invoke();
ResultClass result = synchronizer.get(...)
.invoke();
Spline result = synchronizer.get(...)
.andWakeOthersIf(Spline::isReticulated)
.invoke();| Constructor and Description |
|---|
Synchronizer()
Creates a new
Synchronizer instance. |
| Modifier and Type | Method and Description |
|---|---|
<T> SynchronizerApi.Get.WakeSleepInvoke<T> |
get(@NonNull Supplier<T> getter)
Sets the given
Supplier as the main action of the chain. |
SynchronizerApi.Run.WakeSleepInvoke |
run(@NonNull Runnable runnable)
Sets the given
Runnable as the main action of the chain. |
SynchronizerApi.RunGet |
sleepUntil(@NonNull BooleanSupplier state)
Adds sleeping to the action chain.
|
public SynchronizerApi.Run.WakeSleepInvoke run(@NonNull @NonNull Runnable runnable)
SynchronizerApi.RunGetRunnable as the main action of the chain.run in interface SynchronizerApi.RunGetrunnable - the action to performpublic <T> SynchronizerApi.Get.WakeSleepInvoke<T> get(@NonNull @NonNull Supplier<T> getter)
SynchronizerApi.RunGetSupplier as the main action of the chain.get in interface SynchronizerApi.RunGetgetter - the action to performpublic SynchronizerApi.RunGet sleepUntil(@NonNull @NonNull BooleanSupplier state)
SynchronizerApi.SleepRunGetfalse, this action chain resumes sleeping. Otherwise, the chain
continues by performing the main action.sleepUntil in interface SynchronizerApi.SleepRunGetstate - the supplier that returns true if the action chain should continue, false
otherwise.Copyright © 2021. All rights reserved.