Hey. guys, I want to make a Queue which holds comm...
# coroutines
k
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
Use a Channel
k
But how can you help me on this?
c
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
Is there similar post releated to my work?
c
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
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
You are in channel #coroutines though
k
Yes I am in the channel.
u
you asked in #coroutines, so you’ll get coroutine answers
k
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
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
Yes I am learning basic in
Channel
. If any doubt I'll ask in here. Thanks for great advice.