1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import kotlinx.coroutines.* import java.util.concurrent.CountDownLatch fun main() = runBlocking { val words = listOf("example", "hello", "world", "process", "communication") val latch = CountDownLatch(words.size) for (word in words) { launch(Dispatchers.Default) { val letterCount = countLetters(word) println("Número de letras en '$word': $letterCount") latch.countDown() } } // Esperar a que todas las corutinas terminen latch.await() println("Todas las corutinas han finalizado.") } suspend fun countLetters(word: String): Int = withContext(Dispatchers.Default) { word.length } |