i have a bit of parallel processing along the line...
# coroutines
i have a bit of parallel processing along the lines of
Copy code
val targetFiles = Collections.synchronizedMap(mutableMapOf<String, File>())
val features = Collections.synchronizedList(mutableListOf<SKFeatureComposite>())

val jobs = mutableListOf<Job>()
for(...) {
   jobs += launch {
      //do work here
      targetFile[id] = someFIle
      if(feature != null) {
         features += feature

jobs.forEach { it.join() }
is there cleaner ways to handle collecting data that lets me juggle less mutable collections ?
You might want to consider using
instead of
, then return the
. Then collect the results in the
. To avoid the synchronization when using those collections.
Or use `Channel`s maybe but that might be unnecessary.
well feature might not be there.. so i would have to use a nullable and split it up again later
the annoying part is that its not really a case for
.map {}
I guess for the case of feature, you could use a
Then all the "locking" would be asynchronous.
is there any resources you could recommend to learn about Channels ?
That should do it.
xo i would use 2 seperate channels here, send to each when i get data and then collect it in synchronous code in the end ?
seems too easy /shrug
That's because it is. ­čÖé
That's a more detailed resource if you need one.
thanks for the help.. this is quite helpful
seems to be almost working, for some reason the channel blocks when i call .toMap() or .toList() on it however
apparently the iterator never finishes.. how do i tell it that the channel is done then ?
ok nvm i found out what was wrong.. i used close but tried to iterate over the channel multiple times.. first time seems to consume all data
Yeah, it more of a pipe than an actual collection.
well for your job list, you can surely do targetFiles.map {}?
and then you can call joinAll on the resulting list of jobs
but i'm not exactly sure what variables you need inside each launch { } block so I'm not exactly sure how you should build the list
if it only depends on the loop variable you can do something like List<Job>(some size here) { index -> }
this is based on files that were read from disk, so i aleady have a list of entries, in this case mabe i could have mapped some parts of it to deferred wrappers,, but i'd rather learn how to make it owrk for the harder cases.. which i did.. channels are quite handy