October 2019
Intermediate to advanced
434 pages
11h 54m
English
The Thread class maps directly to the programming concept of a thread of execution within a programming language. Multiple threads can be run at the same time. Therefore, we can create new threads to run code in a non-blocking way. Let's look at a simple example.
To start, we'll define a function called threadExample():
fun threadExample() { println("Running threadExample()") println("Finished threadExample()")}
Within that function, we've added log statements for when the function begins and ends. These are to help illustrate the non-blocking nature of the eventual Thread usage.
Next, we'll create, and start, a new Thread class:
fun threadExample() { println("Running threadExample()") val thread = Thread { Thread.sleep(1000) println ...
Read now
Unlock full access