Synchronized blocks

Sometimes, holding a lock for the entire duration of a function call is too expensive and Java offers to run parts of the code under a lock, again with the synchronized keyword. To achieve the same behavior, Kotlin offers the synchronized function. The function accepts an object that will be locked and a lambda to execute while holding the lock:

fun synchronizedBlock() {
// multiple threads can be here
synchronized(this) {
//only one thread at a time can be here
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.113.150