Methnani Bilel
Methnani Bilel

Reputation: 1385

How to create a Thread pool in Kotlin

I want to create a thread pool in Kotlin. I have been searching for hours in the internet and I can not get a single example. Can anyone provide examples. thank you.

Upvotes: 17

Views: 18219

Answers (1)

Methnani Bilel
Methnani Bilel

Reputation: 1385

    val executor = Executors.newFixedThreadPool(5)

    for (i in 0..9) {
        val worker = Runnable { println("Hello this is thread " + i) }
        executor.execute(worker)
    }

    executor.shutdown()
    executor.awaitTermination(1, TimeUnit.HOURS)
    println("Finished all threads")

Upvotes: 22

Related Questions