Resource releasing

Resource leaking can significantly impact the performance of an application, which is why it's important to release resources after using them. When we work with coroutines, we can just use the try {...} finally {...} block, even with long-term input/output operations.

The following example shows how we can use this block:

fun main(args: Array<String>) {
launch {
val fileReader = FileReader("path")
try {
delay(10000)
fileReader.read()
} finally {
fileReader.close()
}
}
}
..................Content has been hidden....................

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