https://kotlinlang.org logo
#coroutines
Title
# coroutines
k

KotlinLeaner

02/06/2023, 10:13 PM
Hey. guys, I want to make a Queue which holds command. I tried to use
ConcurrentLinkedQueue
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?
setupQueuePolling from the class in my sample project.
Copy code
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")
                        }
                    }
                }
            }
        }
    }
You can find whole class in here QueueViewModel.
k

kevin.cianfarini

02/06/2023, 10:21 PM
Use a Channel
k

KotlinLeaner

02/06/2023, 10:22 PM
But how can you help me on this?
c

Casey Brooks

02/06/2023, 10:36 PM
A Channel is a Queue built on coroutines machinery. This docs page should help you get an understanding of how to use a Channel to build your queue
k

KotlinLeaner

02/06/2023, 10:53 PM
Is there similar post releated to my work?
c

Casey Brooks

02/06/2023, 11:03 PM
You were asking how to make a queue, and how to make it work nicely with coroutines. But the problem is that I don’t think your whole
setupQueuePolling()
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 improves
k

KotlinLeaner

02/06/2023, 11:20 PM
Hi Casey, Thank you for great explanation. I am building an application in which user can read BPM data in mobile. I was reading in some posts that I need to build a queue for that, it run at a time and hold next command in queue until it finish the first job. I used some piece of code from the library. I want to check my existing queue why is it slow? if anything which is more efficient then
ConcurrentLinkedQueue
, then definitely I'll try that. TBH I don't know queue that runs with coroutines will help me on my issue or not..
u

uli

02/08/2023, 12:35 PM
You are in channel #coroutines though
k

KotlinLeaner

02/08/2023, 12:36 PM
Yes I am in the channel.
u

uli

02/08/2023, 12:36 PM
you asked in #coroutines, so you’ll get coroutine answers
k

KotlinLeaner

02/08/2023, 12:38 PM
Yes I want the way in #coroutines . I just explain in above what I need to achieve. Sorry If I did anything wrong in here.
u

uli

02/08/2023, 12:40 PM
Have you looked into the class `Channel`that the coroutines library provides? Otherwise please do so. Channels provide a coroutine based queue implementation with probably better performance characteristics then mixing java threadding in
k

KotlinLeaner

02/08/2023, 12:42 PM
Yes I am learning basic in
Channel
. If any doubt I'll ask in here. Thanks for great advice.
8 Views