-
Notifications
You must be signed in to change notification settings - Fork 24
Channel Types
Devrath edited this page Dec 23, 2023
·
15 revisions
Contents |
|---|
Types of Channels |
Channel Type --> Buffered |
Channel Type --> Conflated |
Channel Type --> Rendezvous |
BufferedConflatedRendezvousUnlimited
- You can observe that we have given the
capacity = 2for theproduceoperator. - So elements sent are a max of 2 elements.
- In the receive you can observe once the earlier 2 elements are received. Then one element is received as the one element is sent by the producer, So until then the second coroutine is suspended.
- Only when one element is received is that space available for the channel buffer to send the data.
- Value
2I passed below is configurable.
private var receiveBufferChannel : ReceiveChannel<Countries> = Channel()
fun usingBuffered() {
// Co-Routine - 1
viewModelScope.launch {
// We are limiting the buffer capacity to 2
receiveBufferChannel = produce(capacity = 2) {
println("Send Action : ----> USA")
send(Countries.USA)
println("Send Action : ----> Russia")
send(Countries.Russia)
println("Send Action : ----> India")
send(Countries.India)
println("Send Action : ----> France")
send(Countries.France)
println("Send Action : ----> Spain")
send(Countries.Spain)
println("Send Action : ----> Germany")
send(Countries.Germany)
println("Send Action : ----> Italy")
send(Countries.Italy)
}
}
// Co-Routine - 2
viewModelScope.launch {
receiveBufferChannel.consumeEach { countries ->
println("Receive Action : ----> $countries")
println("---------------------------------")
delay(3000)
}
}
}
enum class Countries { USA , Russia , India , France , Spain , Germany , Italy }
Send Action : ----> USA
Send Action : ----> Russia
Send Action : ----> India
Receive Action : ----> USA
---------------------------------
Send Action : ----> France
Send Action : ----> Spain
Receive Action : ----> Russia
---------------------------------
Send Action : ----> Germany
Receive Action : ----> India
---------------------------------
Send Action : ----> Italy
Receive Action : ----> France
---------------------------------
Receive Action : ----> Spain
---------------------------------
Receive Action : ----> Germany
---------------------------------
Receive Action : ----> Italy
---------------------------------- Here adding
CONFLATEDto the buffer size of produce indicates that only the latest one is received - Here the capacity is just
1
- Here only the latest value is received no matter how many emissions happen, While others are dropped
private var receiveConflatedChannel : ReceiveChannel<Birds> = Channel()
fun usingConflated() {
// Co-Routine - 1
viewModelScope.launch {
receiveConflatedChannel = produce(capacity = CONFLATED) {
send(Birds.Eagle)
send(Birds.Peacock)
send(Birds.Robin)
send(Birds.Ostrich)
send(Birds.Pigeon)
send(Birds.Kingfisher)
send(Birds.Dodo)
}
}
// Co-Routine - 2
viewModelScope.launch {
receiveConflatedChannel.consumeEach { birds ->
println("BIRD : ----> $birds")
}
}
}
enum class Birds { Eagle , Peacock , Robin , Ostrich , Pigeon , Kingfisher , Dodo }
BIRD : ----> Dodo