Joel Denke
06/15/2023, 7:24 PMPablichjenkov
06/15/2023, 7:26 PMchannel
Joel Denke
06/15/2023, 7:27 PMJoel Denke
06/15/2023, 7:28 PMkevin.cianfarini
06/15/2023, 7:28 PMkevin.cianfarini
06/15/2023, 7:28 PMthats thread safe and able to add new items while consuming queue but only run one operation at the time (Protect another place not thread safe)? Feels like Mutex potentially but not sure.
Joel Denke
06/15/2023, 7:29 PMJoel Denke
06/15/2023, 7:31 PMkevin.cianfarini
06/15/2023, 7:35 PMsuspend fun main() = coroutineScope {
val channel = Channel(Channel.BUFFERED)
launch { channel.consumeValues() }
launch { channel.produceValues() }
}
suspend fun Channel<Int>.consumeValues() {
for (item in this) { // suspends until items available in channel
println(item)
}
}
suspend fun Channel<Int>.produceValues(): Nothing {
var i = 0
while (true) {
delay(100)
send(i++) // suspends until there's space to submit into the channel
}
}
kevin.cianfarini
06/15/2023, 7:35 PMkevin.cianfarini
06/15/2023, 7:35 PMJoel Denke
06/15/2023, 7:37 PMJoel Denke
06/15/2023, 7:38 PMkevin.cianfarini
06/15/2023, 7:39 PMkevin.cianfarini
06/15/2023, 7:40 PMJoel Denke
06/15/2023, 7:41 PMJoel Denke
06/15/2023, 7:42 PMkevin.cianfarini
06/15/2023, 7:42 PMkevin.cianfarini
06/15/2023, 7:43 PMkevin.cianfarini
06/15/2023, 7:44 PMThe channels shown so far had no buffer. Unbuffered channels transfer elements when sender and receiver meet each other (aka rendezvous). If send is invoked first, then it is suspended until receive is invoked, if receive is invoked first, it is suspended until send is invoked.
Joel Denke
06/15/2023, 7:44 PMkevin.cianfarini
06/15/2023, 7:44 PMJoel Denke
06/15/2023, 7:46 PMJoel Denke
06/15/2023, 7:48 PMkevin.cianfarini
06/15/2023, 7:49 PMJoel Denke
06/15/2023, 7:52 PMPablichjenkov
06/15/2023, 7:54 PMkevin.cianfarini
06/16/2023, 12:56 PMchannelFlow
builder and consume them as a flow.
2. You're bridging the non-suspend world to the suspend world. This has consequences; namely, you can't apply backpressure to a producer from a synchronous callback without blocking the thread.kevin.cianfarini
06/16/2023, 12:56 PMkevin.cianfarini
06/16/2023, 12:57 PMPablichjenkov
06/16/2023, 1:37 PMJoel Denke
06/18/2023, 12:15 PMJoel Denke
06/18/2023, 12:17 PMJoel Denke
06/18/2023, 12:19 PMPablichjenkov
06/18/2023, 6:52 PM