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

altavir

04/06/2018, 11:26 AM
I've got a list of objects that have Deferred properties which produce some kind of reult, afteer result is produced, the Deferred is replaced by another (Completable)Deferred. Now I want to create a listener which will perform some action when any future is completed. I am trying to do so using
select
clause this way:
Copy code
val job = launch {
            while (true) {
                select<Unit> {
                    stateHolder.forEach { state ->
                        state.future.onAwait {
                                // do something
                        }
                    }
                }
            }
        }
But it does not seem to enter the select clause even once. What am I doing wrong?
g

gildor

04/06/2018, 2:38 PM
Did you try to debug?
a

altavir

04/06/2018, 5:29 PM
Of course. I found it to be a some bizarre synchronization problem, which i did not expect to encounter in coroutines. Everything works fine if I introduce blocking sleep between state changes which are done from outside of coronations. Probably should use
Mutex
locks. In fact, I keep thinking that it should be simpler. All I need is to create an observable mutable state with coroutine based subscribers.
It seems that I really need
BroadcastChannel
. I missed it previously.
g

gildor

04/07/2018, 2:13 AM
Coroutines do not solve syncronous problems if you have shared mutable states between threads, bit with coroutines you can use actors or other patterns to handle this
a

altavir

04/07/2018, 2:01 PM
I do not have problems with concurrent state changes, the problem was with notifications. I finally solved it with
BroadcastChannel
. Everything seems to be working fin now.
3 Views