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

pajatopmr

02/01/2019, 8:39 AM
“Is it secret Frodo? Is it SAFE?” So I have this piece of code (first serious coroutine hacking I’ve done) where I have a map associating names with lists of data. The main thread will allow clients to access the lists while a daily background thread will update the data. Main thread is read-only, Background thread is write-only. I think, from an app perspective, that this code is safe even if not predictable. So am I correct?
o

octylFractal

02/01/2019, 8:43 AM
only threadsafe if
data
is thread safe, I think. if
data
is accessed concurrently with a non-thread-safe-map like HashMap, it's likely to to throw ConcurrentModificationException or worse, fail silently. reason being is that you are accessing
data
on two threads, the
<http://Disp.IO|Disp.IO>
threads and your main threads. even if it's not writing on both, it's still problematic for HashMap.
👍 1
m

marstran

02/01/2019, 8:45 AM
You should share the data by communicating, not by sharing mutable data. I think you should use an
actor
.
p

pajatopmr

02/01/2019, 8:54 AM
@marstran do you have a link handy?
m

marstran

02/01/2019, 8:56 AM
You could check out Roman's talk from Kotlinconf last year:

https://www.youtube.com/watch?v=a3agLJQ6vt8

p

pajatopmr

02/01/2019, 8:57 AM
And I just came across
actor
in the link @octylFractal sent.
m

marstran

02/01/2019, 8:58 AM
@octylFractal Yes, but there will be a replacement. Actors as a concept is not obsolete.
o

octylFractal

02/01/2019, 8:59 AM
mhm. I only meant the specific API point
p

pajatopmr

02/01/2019, 9:24 AM
Yes, it is very clear from https://github.com/Kotlin/kotlinx.coroutines/issues/87 that the
actor
model is useful but could be better (support a more complex model). So it looks like a decent choice to fix my issue.
e

elizarov

02/01/2019, 9:29 AM
Sharing
MutableMap
in this way is not safe.
3 Views