August 2017
Intermediate to advanced
440 pages
10h
English
Sometimes, we need to mark part of the code to be executed in a different way. The thread function is this kind of situation. We need some code to be executed asynchronously, so we surround it with brackets, starting from the thread function:
thread {
operation1()
operation2()
}
From the outside, it looks as if it is a part of code that is surrounded by a block named thread. Let's look at another example. Let's suppose that we want to log the execution time of a certain code block. As a helper, we will define the addLogs function, which will print logs together with the execution time. We will define it in the following way:
fun addLogs(tag: String, f: () -> Unit) { println("$tag started") val startTime = System.currentTimeMillis() ...Read now
Unlock full access