io.justdevit.kotlin.boost.extension.Lock.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of boost-commons Show documentation
Show all versions of boost-commons Show documentation
Library to boost working with Kotlin/JVM projects.
The newest version!
package io.justdevit.kotlin.boost.extension
import java.util.concurrent.locks.Lock
/**
* Runs the specified [action] if the given [predicate] evaluates to `true`, while ensuring exclusive access to the
* critical section by acquiring the lock before running the action and releasing the lock afterwards.
*
* @param predicate A function that evaluates the condition for executing the [action].
* @param action The operation to perform if the [predicate] evaluates to `true`.
*/
inline fun Lock.runIf(predicate: () -> Boolean, action: () -> Unit) {
if (predicate()) {
lock()
try {
if (predicate()) {
action()
}
} finally {
unlock()
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy