Skip to content

Commit

Permalink
03_05_GERENCIANDO_TAREFAS_ASSINCRONAS_COM_COROTINAS
Browse files Browse the repository at this point in the history
  • Loading branch information
DevEgF committed Dec 7, 2024
0 parents commit b59deff
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Kotlin ###
.kotlin

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/libraries/KotlinJavaRuntime.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/jetbrains_kotlinx_coroutines_core.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions ConhecendoCoroutines.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/testResources" type="java-test-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
<orderEntry type="library" name="jetbrains.kotlinx.coroutines.core" level="project" />
</component>
</module>
42 changes: 42 additions & 0 deletions src/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import kotlinx.coroutines.*
import java.text.SimpleDateFormat
import java.util.*

// Objetivo: construção de uma casa, trabalhadores operando para contruir uma casa. Subir as paredes e instalar
// as portas, janelas e telhado.

enum class BuildingMaterial(val description: String, val veliveringTimeInMillis: Long) {
DOORS("PORTAS", 500),
WINDOWS("JANELAS", 1_000),
ROOF("TELHADO", 3_000),
}

fun getTimeNow(): String =
SimpleDateFormat("hh:mm:ss:SSS", Locale.getDefault()).format(Date())

suspend fun order(material: BuildingMaterial): BuildingMaterial {
println("${getTimeNow()} >>>>> SOLICITANDO ${material.description}...")
delay(material.veliveringTimeInMillis)
println("${getTimeNow()} >>>>> ${material.description} CHEGARAM!")
return material
}

suspend fun doWork(task: String) {
println("${getTimeNow()} >>>>> EXECUTANDO TAREFA [$task]...")
delay(1_000)
println("${getTimeNow()} >>>>> TAREFA [$task] CONCLUÍDA!")
}

fun main() {
runBlocking {
val doors = async { order(BuildingMaterial.DOORS) }
val roof = async { order(BuildingMaterial.ROOF) }
val windows = async { order(BuildingMaterial.WINDOWS) }

doWork("CONSTRUIR PAREDES")
doWork("CONSTRUIR PAREDES")
launch { doWork("INSTALAR ${doors.await().description}") }
launch { doWork("INSTALAR ${roof.await().description}") }.cancel()
launch { doWork("INSTALAR ${windows.await().description}") }
}
}

0 comments on commit b59deff

Please sign in to comment.