Usage of higher-order functions can lead to a decrease of runtime performance. Memory allocations of the functions passed as lambda arguments and their virtual calls in a function body lead to runtime overhead. However, in many cases, we can eliminate this type of overhead by inlining the lambda expression parameters.
In this recipe, we are going to implement the lock() function that will automate work with the Java java.util.concurrent.locks.Lock interface. The function will take two arguments—an instance of the Lock interface and the function that should be invoked after the lock is acquired. Finally, our lock() function should release the lock. We also want to allow making the function parameter inlined.