How to initialize a Thread in Kotlin?

后端 未结 9 2268
青春惊慌失措
青春惊慌失措 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 22:09

    fun main(args: Array) {
    
        Thread({
            println("test1")
            Thread.sleep(1000)
        }).start()
    
        val thread = object: Thread(){
            override fun run(){
                println("test2")
                Thread.sleep(2000)
            }
        }
    
        thread.start()
    
        Thread.sleep(5000)
    }
    

提交回复
热议问题