https://kotlinlang.org logo
Title
c

coder82

05/31/2019, 8:07 AM
How could I check if a singlethreaded coroutine context is currently executing or not?
I creatd it with newSingleThreadContext("name")
g

gildor

05/31/2019, 9:15 AM
What is your use case for it?
c

coder82

05/31/2019, 9:22 AM
I want a single thread to which I queue tasks, and if a task is already executing I don't want to queue another task, rather, do something else like printing a message
Essentially, I have 2 tasks, and they must go 1 at a time, if user tries to execute them in parallel I need to print a message.
So I thought to check "isActive" on a shared single thread context, but it doesn't work
g

gildor

05/31/2019, 9:24 AM
Oh, I think that it’s completely wrong approach to queue coroutine tasks
You cannot use single thread for this
because coroutine do not block thread, this would make the whole idea of asynchronous API useless
so obviously you cannot limit amout of running coroutine on thread
instead of thread you can use actor, which is just a single coroutine that has message channel and process each of them sequentially
I don’t want to queue another task
If you don’t need queue, just 1 is running and all other are dropped, you can use
offer
method which returns false if channel is already full and message will be just lost
c

coder82

05/31/2019, 9:27 AM
yep makes sense