File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
Roadmap/15 - ASINCRONÍA/kotlin Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments