const classafConcurrent::Synchronized
sys::Obj afConcurrent::Synchronized
Provides synchronized access to blocks of code. Example usage:
lock := Synchronized(ActorPool())
val := lock.synchronized |->Obj?| {
// ...
// important stuff
// ...
return 69
}
IoC Injection
The Concurrent library defines an IoC SynchronizedProvider that injects Synchronized instances directly into your class. To use, ensure the id field is set to a contributed ActorPool:
const class Example {
@Inject { id="actorPoolId" }
const Synchronized lock
new make(|This|in) { in(this) }
}
- async
virtual Future async(|->Obj? f)Runs the given func asynchronously.
Errs that occur within the block are logged but not re-thrown unless you call
get()on the returnedFuture.The given func and return value must be immutable.
- asyncLater
virtual Future asyncLater(Duration d, |->Obj? f)Runs the given func asynchronously, after the given duration has elapsed.
Errs that occur within the block are logged but not rethrown unless you call
get()on the returnedFuture.The given func and return value must be immutable.
- inSync
Bool inSync()Returns
trueif the current thread is running inside the synchronised Actor. E.g.:lock := Synchronized(ActorPool()) lock.inSync
// --> falselock.synchronized |->| { lock.inSync// --> true... }- make
new make(ActorPool actorPool, Duration? timeout := null, |This? f := null)Create a
Synchronizedclass from the givenActorPooland timeout.The default timeout of
nullblocks forever.- reentrant
const Bool reentrant := trueDetermines if this synchronised lock is re-entrant or not. Re-entrant locks allow multiple nested calls to
synchronized()(on this object) without fear of deadlocks.Because re-entrant locks are often considered an indication of bad design, setting
reentranttofalsewill disable nested calls tosynchronized(), throwing an Err instead.Defaults to
true.- sync
Alias for
synchronized().Effectively wraps the given func in a Java
synchronized { ... }block and returns its calculated value.The given func and return value must be immutable.
- synchronized
virtual Obj? synchronized(|->Obj? f)This effectively wraps the given func in a Java
synchronized { ... }block and returns its calculated value.The given func and return value must be immutable.
- timeout
const Duration? timeoutThe default timeout to use when waiting for
synchronizedblocks to complete.The default timeout of
nullblocks forever.