Class Synchronizer
java.lang.Object
com.github.mizool.core.concurrent.Synchronizer
- All Implemented Interfaces:
SynchronizerApi.Fluent,SynchronizerApi.RunGet,SynchronizerApi.SleepRunGet
Encapsulates synchronization and wait/notify functionality.
With its fluent API and encapsulated lock, this class can help increasing both the readability and robustness of concurrent algorithms. Still, care must be taken to avoid deadlocks, just as if using

Introduction
By calling any of the instance methods ofSynchronizer, 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.With its fluent API and encapsulated lock, this class can help increasing both the readability and robustness of concurrent algorithms. Still, care must be taken to avoid deadlocks, just as if using
synchronized blocks
and Object.wait() / Object.notifyAll() directly.Actions
Synchronizer provides the following actions:- Main action
- Sleep until condition is
true-
Whenever the thread receives a wake call, it checks this condition. If
true, the thread stops sleeping and continues its action chain. Iffalse, it resumes sleeping. - By default, the condition will only be checked when a wake call happens. However, you can also specify the duration of an interval after which the thread should re-check the condition on its own and then stop/resume sleep as explained above.
-
Can be used before (
sleepUntil) and/or after (thenSleepUntil) the main action. -
Sleeping is implemented in line with secure coding practices, i.e.
Object.wait()is called inside a loop to ensure liveness and safety (see SEI CERT rule THI03-J for details). -
If the thread is interrupted while waiting,
UncheckedInterruptedExceptionis thrown and the thread's interrupted flag is re-set.
-
Whenever the thread receives a wake call, it checks this condition. If
- Wake other threads
-
Wakes each sleeping thread (by calling
Object.notifyAll()), causing it to check its condition (see above). -
If the main action of this chain is a
Supplier<T>, the calling code can decide whether waking takes place by usingandWakeOthersIf(Predicate<T>)instead.
-
Wakes each sleeping thread (by calling
Fluent API overview
Example usage
synchronizer.run(...)
.thenSleepUntil(...)
.invoke();
synchronizer.sleepUntil(...)
.run(...)
.andWakeOthers()
.invoke();
ResultClass result = synchronizer.get(...)
.invoke();
Spline result = synchronizer.get(...)
.andWakeOthersIf(Spline::isReticulated)
.invoke();
Note on terminology
This class intentionally uses the verbs "sleep" and "wake". If it used "wait" and "notify" instead, its methods would all too easily be confused with methods in java.lang.Object which, if invoked on the objects returned by chained methods, could cause deadlocks.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionSets the givenSupplieras the main action of the chain.Sets the givenRunnableas the main action of the chain.sleepUntil(@NonNull BooleanSupplier state, Duration checkInterval) Adds sleeping to the action chain.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.github.mizool.core.concurrent.SynchronizerApi.SleepRunGet
sleepUntil, wakeOthers
-
Constructor Details
-
Synchronizer
public Synchronizer()Creates a newSynchronizerinstance.
-
-
Method Details
-
run
Description copied from interface:SynchronizerApi.RunGetSets the givenRunnableas the main action of the chain.- Specified by:
runin interfaceSynchronizerApi.RunGet- Parameters:
runnable- the action to perform
-
get
Description copied from interface:SynchronizerApi.RunGetSets the givenSupplieras the main action of the chain.- Specified by:
getin interfaceSynchronizerApi.RunGet- Parameters:
getter- the action to perform
-
sleepUntil
public SynchronizerApi.RunGetInvoke sleepUntil(@NonNull @NonNull BooleanSupplier state, Duration checkInterval) Description copied from interface:SynchronizerApi.SleepRunGetAdds sleeping to the action chain.
This action ensures the given condition is met before performing the next action. When this action begins, the supplier is called immediately.- Supplier returns
false: -
The chain sleeps until either another chain wakes it up or the
checkIntervalhas passed.
In both cases, the supplier is then called again and if it still returnsfalse, the chain resumes sleeping. - Supplier returns
true: - The chain proceeds to perform the next action.
- Specified by:
sleepUntilin interfaceSynchronizerApi.SleepRunGet- Parameters:
state- the supplier that returnstrueif the action chain should continue,falseotherwise.checkInterval- how often the condition should be re-checked even if no wake call happens, ornullfor "never".
- Supplier returns
-