Skip to content

Commit

Permalink
Rendez-vous channel implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperancinha committed Jan 15, 2024
1 parent fb77bc1 commit af8ca58
Showing 1 changed file with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.jesperancinha.ktd

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import java.util.*
import java.util.concurrent.LinkedBlockingQueue


private suspend fun fetchItem() = withContext(Dispatchers.Default) { "ITEM-${UUID.randomUUID()}" }
private const val PRODUCTION_COST_MS = 200L

class ElementsSendReceiveBlockingChannel {

private val linkedBlockingQueue by lazy { LinkedBlockingQueue<String>(100) }
private suspend fun produceItems() {
val item = fetchItem()
delay(PRODUCTION_COST_MS)
linkedBlockingQueue.add(item)
delay(PRODUCTION_COST_MS)
linkedBlockingQueue.add(item)
}

private suspend fun receiveItems() {
do {
val item = withContext(Dispatchers.IO) {
linkedBlockingQueue.take()
}
println("LinkedBlockingQueue received item $item!")
} while (!linkedBlockingQueue.isEmpty())
}

suspend fun doMain() = coroutineScope {
val receiveTask = async { receiveItems() }
val produceTask = async { produceItems() }
produceTask.await()
receiveTask.await()
}
}

class ElementsSendReceiveChannel {

private val channel by lazy { Channel<String>(2) }

private suspend fun produceItems() {
val item = fetchItem()
delay(PRODUCTION_COST_MS)
channel.send(item)
delay(PRODUCTION_COST_MS)
channel.send(item)
}


private suspend fun receiveItems() {
repeat(2) {
val item = channel.receive()
println("Channel received item $item!")
}
}

suspend fun doMain() = coroutineScope {
launch { produceItems() }
launch { receiveItems() }
}
}

class ElementsSendReceiveRunner {
companion object {
@JvmStatic
fun main(args: Array<String>): Unit = runBlocking {
ElementsSendReceiveBlockingChannel().doMain()
ElementsSendReceiveChannel().doMain()
}
}
}

0 comments on commit af8ca58

Please sign in to comment.