KotlinLeaner
02/06/2023, 10:13 PMConcurrentLinkedQueue
with <http://Dispatcher.IO|Dispatcher.IO>
and synchronized
with ConcurrentLinkedQueue
. The code is working fine but very slow responding the data. Anyone guide me on this, How to improve response time?private fun setupQueuePolling() {
viewModelScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
Log.e(TAG, "Starting Polling")
while (true) {
synchronized(commandQueue) {
if (!commandQueue.isEmpty()) {
commandQueue.poll()?.let { qItem ->
qItem("This is input")
}
}
}
}
}
}
kevin.cianfarini
02/06/2023, 10:21 PMKotlinLeaner
02/06/2023, 10:22 PMCasey Brooks
02/06/2023, 10:36 PMKotlinLeaner
02/06/2023, 10:53 PMCasey Brooks
02/06/2023, 11:03 PMsetupQueuePolling()
isn’t quite doing what you expect. ConcurrentLinkedQueue
and synchronized()
are Java concurrency mechanisms which don’t play nicely with coroutines, so we suggested an alternative which does work with coroutines. Your original question stated “I tried to use”, which suggested that you weren’t completely bought into that solution yet
Is your main question how to build a queue that runs on coroutines, or help diagnosing why your existing queue is slow? It’s difficult to know what is making it slow without knowing exactly what you mean by “slow”. But if you’re still just exploring this queue mechanism more generally, I would recommend replacing your ConcurrentLinkedQueue
with a Channel
first, and then see if the slowness improvesKotlinLeaner
02/06/2023, 11:20 PMConcurrentLinkedQueue
, then definitely I'll try that. TBH I don't know queue that runs with coroutines will help me on my issue or not..uli
02/08/2023, 12:35 PMKotlinLeaner
02/08/2023, 12:36 PMuli
02/08/2023, 12:36 PMKotlinLeaner
02/08/2023, 12:38 PMuli
02/08/2023, 12:40 PMKotlinLeaner
02/08/2023, 12:42 PMChannel
. If any doubt I'll ask in here. Thanks for great advice.