-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a1a371
commit cdf30a1
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...utines/coroutines-crums-group-1/src/main/kotlin/org/jesperancinha/ktd/TicketCollectors.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.jesperancinha.ktd | ||
|
||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.runBlocking | ||
import kotlin.random.Random | ||
|
||
class TicketCollectors { | ||
companion object { | ||
@JvmStatic | ||
fun main(args: Array<String> = emptyArray()) = runBlocking { | ||
flow { | ||
repeat(10) { | ||
emit(Random.nextInt(10, 25)) | ||
delay(2) | ||
} | ||
}.collect { value -> | ||
processTemperature(value) | ||
} | ||
println("Finished processing collect!") | ||
|
||
flow { | ||
repeat(10) { | ||
emit(Random.nextInt(10, 25)) | ||
delay(2) | ||
} | ||
}.collectLatest { value -> | ||
processTemperature(value) | ||
} | ||
println("Finished processing collectLatest!") | ||
|
||
} | ||
|
||
private suspend fun processTemperature(value: Int) { | ||
println("Processing-> $value") | ||
delay(Random.nextLong(1, 3)) | ||
println("Processed!-> $value") | ||
|
||
} | ||
} | ||
} |