Skip to content

Commit 000da71

Browse files
authored
Merge pull request mouredev#5793 from eulogioep/mi-solucion-reto-06
#15 - Kotlin
2 parents 9cc99ce + fde84c6 commit 000da71

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import kotlinx.coroutines.*
2+
import java.time.LocalTime
3+
import java.time.format.DateTimeFormatter
4+
5+
suspend fun asyncFunction(name: String, duration: Int) {
6+
val formatter = DateTimeFormatter.ofPattern("HH:mm:ss")
7+
8+
println("$name: Iniciando en ${LocalTime.now().format(formatter)}")
9+
println("$name: Duración de $duration segundos")
10+
11+
delay(duration * 1000L)
12+
13+
println("$name: Finalizando en ${LocalTime.now().format(formatter)}")
14+
}
15+
16+
suspend fun main() = coroutineScope {
17+
// Parte básica del ejercicio
18+
launch { asyncFunction("Función de ejemplo", 5) }
19+
20+
// Parte extra del ejercicio
21+
val jobC = launch { asyncFunction("Función C", 3) }
22+
val jobB = launch { asyncFunction("Función B", 2) }
23+
val jobA = launch { asyncFunction("Función A", 1) }
24+
25+
// Esperamos a que las funciones A, B y C terminen
26+
jobC.join()
27+
jobB.join()
28+
jobA.join()
29+
30+
// Ejecutamos la función D después de que A, B y C hayan terminado
31+
asyncFunction("Función D", 1)
32+
}

0 commit comments

Comments
 (0)