How to initialize a Thread in Kotlin?

后端 未结 9 2252
青春惊慌失措
青春惊慌失措 2021-02-06 21:14

In Java it works by accepting an object which implements runnable :

Thread myThread = new Thread(new myRunnable())

where myRunnable

9条回答
  •  臣服心动
    2021-02-06 21:58

    Basic example of Thread with Lamda

    fun main() {
        val mylamda = Thread({
            for (x in 0..10){
                Thread.sleep(200)
                println("$x")
            }
       })
        startThread(mylamda)
    
    }
    
    fun startThread(mylamda: Thread) {
        mylamda.start()
    }
    

提交回复
热议问题