Search code examples
kotlinasynchronousbatch-processingbatch-normalization

Run 10 batch job in parallel kotlin


I have one batch, that makes some job in a sync method. It can take some time. For optimizing, I want to do create async method, that will execute 10 batches in one time in parallel. Is it possible in Kotlin?


Solution

  • This totally depends on the type of your task, do you want to execute multiple CPU boung long running tasks, then you should go with ExecutorService. On the other hand if you want to execute multiple async calls which are not as much CPU intensive, then you can look into kotliln coroutines

    Just to give an example, when using corotuines first you need to define your task, let say you have a suspending function for it, as following

    suspend fun myTask(param: SomeParam)
    

    Now you want to start execution of 10 tasks in parallel, then you do

    runBlocking {  // start a coroutine it is like a thread, but very lightweight
       launch(Dispatcher.Default){ myTask(param) }    // launch individual coroutine for every parallel task
       launch(Dispatcher.Default){ myTask(someOtherParam) }
       ...
       launch(Dispatcher.Default){ myTask(yetAnotherParam) }
       //  Choose appropriate dispatcher (Main, IO or Default)
    }
    

    launch is a coroutine builder which starts a coroutine without blocking the calling thread. you can also use async if you want to return some result.

    There are lots of options available, I suggest you look into Coroutines guide