https://kotlinlang.org logo
#coroutines
Title
# coroutines
d

dstarcev

03/23/2017, 1:05 PM
what is similar to C#‘s
Task.WhenAll()
in kotlin?
d

deviant

03/23/2017, 2:49 PM
if you tell us what this method means in c# 😉
k

krotki

03/23/2017, 6:25 PM
i'm not sure if this is exactly what you need but WhenAll sounds like CompletableFuture.allOf
and small example
Copy code
import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.future.future
import java.util.concurrent.CompletableFuture
import kotlin.system.measureTimeMillis

fun main(args: Array<String>) {
    val time1 = measureTimeMillis {
        listOf(1, 2, 3)
                .map { i -> future { delay(i * 1000L); 42 } }
                .map { it.get() }
    }
    println("Completed in $time1 ms")

    val time2 = measureTimeMillis {
        val tasks = listOf(1, 2, 3)
                .map { i -> future { delay(i * 1000L); 42 } }
        CompletableFuture.allOf(*tasks.toTypedArray()).get()
    }
    println("Completed in $time2 ms")
}
5 Views