In Java it works by accepting an object which implements runnable :
Thread myThread = new Thread(new myRunnable())
where myRunnable
Best way would be to use thread()
generator function from kotlin.concurrent
:
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.concurrent/thread.html
You should check its default values, as they're quite useful:
thread() { /* do something */ }
Note that you don't need to call start()
like in the Thread example, or provide start=true
.
Be careful with threads that run for a long period of time. It's useful to specify thread(isDaemon= true)
so your application would be able to terminate correctly.
Usually application will wait until all non-daemon threads terminate.