Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Thread pool in Kotlin

Tags:

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.

like image 985
Methnani Bilel Avatar asked May 20 '17 01:05

Methnani Bilel


People also ask

How do you make a thread pool?

To use thread pools, we first create a object of ExecutorService and pass a set of tasks to it. ThreadPoolExecutor class allows to set the core and maximum pool size. The runnables that are run by a particular thread are executed sequentially.

How do you create a thread on Kotlin?

Creating a thread in Kotlin is similar to doing so in Java. class SimpleThread: Thread() { public override fun run() { println("${Thread. currentThread()} has run.") } } Or we can implement the Runnable interface: class SimpleRunnable: Runnable { public override fun run() { println("${Thread.

How does a thread pool work?

In computer programming, a thread pool is a software design pattern for achieving concurrency of execution in a computer program. Often also called a replicated workers or worker-crew model, a thread pool maintains multiple threads waiting for tasks to be allocated for concurrent execution by the supervising program.

What is thread pool in Android?

Android in Practice A thread pool is a set of threads that are managed in a controlled environment, for instance by setting an upper limit on the number of threads and by forcing the application to reuse threads and distribute the workload among them.


1 Answers

    val executor = Executors.newFixedThreadPool(5)
    for (i in 0..9) {
        val worker = Runnable { println("Hello this is thread " + i) }
        executor.execute(worker)
    }
    executor.shutdown()
    while (!executor.isTerminated) {
    }
    println("Finished all threads")
like image 117
Methnani Bilel Avatar answered Oct 03 '22 12:10

Methnani Bilel