Skip to content

Instantly share code, notes, and snippets.

@Miha-x64
Last active October 29, 2019 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Miha-x64/6d31a8c9f5191a7303158aaba7310158 to your computer and use it in GitHub Desktop.
Save Miha-x64/6d31a8c9f5191a7303158aaba7310158 to your computer and use it in GitHub Desktop.
Index: app/src/main/java/com/techyourchance/multithreading/demonstrations/designcoroutines/ProducerConsumerBenchmarkUseCase.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- app/src/main/java/com/techyourchance/multithreading/demonstrations/designcoroutines/ProducerConsumerBenchmarkUseCase.kt (revision ce962bc96a3224ae27ff8c94ba658946362ce852)
+++ app/src/main/java/com/techyourchance/multithreading/demonstrations/designcoroutines/ProducerConsumerBenchmarkUseCase.kt (date 1572347476000)
@@ -1,17 +1,16 @@
package com.techyourchance.multithreading.demonstrations.designcoroutines
-import android.os.Handler
-import android.os.Looper
import android.util.Log
import com.techyourchance.multithreading.DefaultConfiguration
import kotlinx.coroutines.*
+import kotlinx.coroutines.channels.Channel
import java.util.concurrent.atomic.AtomicInteger
class ProducerConsumerBenchmarkUseCase {
class Result(val executionTime: Long, val numOfReceivedMessages: Int)
- private val blockingQueue = MyBlockingQueue(BLOCKING_QUEUE_CAPACITY)
+ private val queue = Channel<Int>(0)
private val numOfReceivedMessages: AtomicInteger = AtomicInteger(0)
private val numOfProducers: AtomicInteger = AtomicInteger(0)
@@ -55,13 +54,13 @@
Log.d("Producer", "producer ${numOfProducers.incrementAndGet()} started; " +
"on thread ${Thread.currentThread().name}");
Thread.sleep(DefaultConfiguration.DEFAULT_PRODUCER_DELAY_MS.toLong())
- blockingQueue.put(index)
+ queue.send(index)
}
private fun CoroutineScope.startNewConsumer() = launch(Dispatchers.IO) {
Log.d("Consumer", "consumer ${numOfConsumers.incrementAndGet()} started; " +
"on thread ${Thread.currentThread().name}");
- val message = blockingQueue.take()
+ val message = queue.receive()
if (message != -1) {
numOfReceivedMessages.incrementAndGet()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment