-
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
fb77bc1
commit af8ca58
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
...utines-crums-group-1/src/main/kotlin/org/jesperancinha/ktd/ElementsSendReceiveChannels.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,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() | ||
} | ||
} | ||
} |