How to initialize a Thread in Kotlin?

Shubhranshu Jain from

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

Thread myThread = new Thread(new myRunnable())

where myRunnable is a class implementing Runnable.

But when I tried this in Kotlin, it doesn't seems to work:

var myThread:Thread = myRunnable:Runnable

java multithreading kotlin


answered 10 months ago Rajesh Dalsaniya #1


val myRunnable = runnable {



// call runnable here
  println("running from lambda: ${Thread.currentThread()}")

You don't see a Runnable here: in Kotlin it can easily be replaced with a lambda expression. Is there a better way? Sure! Here's how you can instantiate and start a thread Kotlin-style:

thread(start = true) {  
      println("running from thread(): ${Thread.currentThread()}")

answered 10 months ago s1m0nw1 #2

For initialising an object of Thread you simply invoke the constructor:

val t = Thread()

Then, you can also pass an optional Runnable with a lambda (works due to SAM Conversion) like this:

Thread {

The more explicit version is passing an anonymous implementation of Runnable like this:

Thread(Runnable {

Note that the previously shown examples do only create an instance of a Thread but don't actually start it. In order to achieve that, you need to invoke start() explicitly.

Last but not least, you need to know the standard library function thread, which usage I'd recommend:

public fun thread(start: Boolean = true, isDaemon: Boolean = false, contextClassLoader: ClassLoader? = null, name: String? = null, priority: Int = -1, block: () -> Unit): Thread {


thread(start = true) {

It has many optional parameters for e.g. directly starting the thread as shown here.

answered 7 months ago Hardeep singh #3

Please try this code:

Thread().run { Thread.sleep(3000); }

answered 5 months ago Alexey Soshin #4

Best way would be to use thread() generator function from kotlin.concurrent:

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.

answered 6 days ago Adam Hurwitz #5

I did the following and it appears to be working as expected.

Thread(Runnable {
            //some method here