Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3667ea0

Browse files
Add playground files for buffer overflows
1 parent 7b2db73 commit 3667ea0

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.flow.concurrency
2+
3+
import kotlinx.coroutines.channels.BufferOverflow
4+
import kotlinx.coroutines.coroutineScope
5+
import kotlinx.coroutines.delay
6+
import kotlinx.coroutines.flow.buffer
7+
import kotlinx.coroutines.flow.flow
8+
9+
suspend fun main() = coroutineScope {
10+
11+
val flow = flow {
12+
repeat(5) {
13+
val pancakeIndex = it + 1
14+
println("Emitter: Start Cooking Pancake $pancakeIndex")
15+
delay(100)
16+
println("Emitter: Pancake $pancakeIndex ready!")
17+
emit(pancakeIndex)
18+
}
19+
}.buffer(capacity = 1, onBufferOverflow = BufferOverflow.SUSPEND)
20+
21+
flow.collect {
22+
println("Collector: Start eating pancake $it")
23+
delay(300)
24+
println("Collector: Finished eating pancake $it")
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.flow.concurrency
2+
3+
import kotlinx.coroutines.channels.BufferOverflow
4+
import kotlinx.coroutines.coroutineScope
5+
import kotlinx.coroutines.delay
6+
import kotlinx.coroutines.flow.buffer
7+
import kotlinx.coroutines.flow.flow
8+
9+
suspend fun main() = coroutineScope {
10+
11+
val flow = flow {
12+
repeat(5) {
13+
val pancakeIndex = it + 1
14+
println("Emitter: Start Cooking Pancake $pancakeIndex")
15+
delay(100)
16+
println("Emitter: Pancake $pancakeIndex ready!")
17+
emit(pancakeIndex)
18+
}
19+
}.buffer(capacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
20+
21+
flow.collect {
22+
println("Collector: Start eating pancake $it")
23+
delay(300)
24+
println("Collector: Finished eating pancake $it")
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.flow.concurrency
2+
3+
import kotlinx.coroutines.channels.BufferOverflow
4+
import kotlinx.coroutines.coroutineScope
5+
import kotlinx.coroutines.delay
6+
import kotlinx.coroutines.flow.buffer
7+
import kotlinx.coroutines.flow.flow
8+
9+
suspend fun main() = coroutineScope {
10+
11+
val flow = flow {
12+
repeat(5) {
13+
val pancakeIndex = it + 1
14+
println("Emitter: Start Cooking Pancake $pancakeIndex")
15+
delay(100)
16+
println("Emitter: Pancake $pancakeIndex ready!")
17+
emit(pancakeIndex)
18+
}
19+
}.buffer(capacity = 1, onBufferOverflow = BufferOverflow.DROP_LATEST)
20+
21+
flow.collect {
22+
println("Collector: Start eating pancake $it")
23+
delay(300)
24+
println("Collector: Finished eating pancake $it")
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.flow.concurrency
2+
3+
import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED
4+
import kotlinx.coroutines.coroutineScope
5+
import kotlinx.coroutines.delay
6+
import kotlinx.coroutines.flow.buffer
7+
import kotlinx.coroutines.flow.flow
8+
9+
suspend fun main() = coroutineScope {
10+
11+
val flow = flow {
12+
repeat(5) {
13+
val pancakeIndex = it + 1
14+
println("Emitter: Start Cooking Pancake $pancakeIndex")
15+
delay(100)
16+
println("Emitter: Pancake $pancakeIndex ready!")
17+
emit(pancakeIndex)
18+
}
19+
}.buffer(capacity = UNLIMITED)
20+
21+
flow.collect {
22+
println("Collector: Start eating pancake $it")
23+
delay(300)
24+
println("Collector: Finished eating pancake $it")
25+
}
26+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /