Let's declare a lock() function with two arguments—an instance of the Lock interface and the function to be invoked after the lock is acquired:
inline fun performHavingLock(lock: Lock, task: () -> Unit) {
lock.lock()
try {
task()
}
finally {
lock.unlock()
}
}