To initialize the object, Threadyou simply call the constructor:
val t = Thread()
Then you can also pass an optional Runnablewith a lambda expression (SAM conversion) as follows:
Thread {
Thread.sleep(1000)
println("test")
}
A more explicit version conveys an anonymous implementation Runnableas follows:
Thread(Runnable {
Thread.sleep(1000)
println("test")
})
Note that the previously shown examples only create an instance Threadbut do not actually start it. For this you need to explicitly call start().
, : thread , :
public fun thread(start: Boolean = true, isDaemon: Boolean = false, contextClassLoader: ClassLoader? = null, name: String? = null, priority: Int = -1, block: () -> Unit): Thread {
:
thread(start = true) {
Thread.sleep(1000)
println("test")
}
, , , . , true start.