I’m trying to to the following function that can get list of suspend lamdas run all of them in parallel and wait for the first one that will finish?, in case that I had the blocks not in list I could just use select<>{} but for my problem i get the suspend blocks in list,
Code always will be better:
typealias SUSPEND_METHOD = suspend () -> Unit
fun waitForFirst(tasks: List<SUSPEND_METHOD>) {
// need the help here, how to implement it
}
Hi @sabagronen,
issue 59 is really use-case sensitive, I don’t know how this apply to your use case.
However Job.onAwait in a select allow you to wait only for ther first Job, so you should iterate to wait for n jobs.
Finally in linked issue 58 “alek-sys” propose an interesting implementation using a Channel: “Use counter + done channel”, in such case it is easy to get/await only n results.
PS: totalJobs = AtomicInteger should be a regular Int, in your use case must be “2”.
Actually, my use case is to write a class that knows, among other things, to run x tasks in parallel and waits for y of them to finish, where y<=x. For waiting to all I’ve put all the Jobs in a list and apply join() on each one of them, for one thanks to you I figured it up how to do it.
If the way to wait for 2 is working, I get wait for any y. I wonder what is the preferred way for waitForFirst since we can iterate until the first