i have a bit of parallel processing along the line...
# coroutines
n
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 ?
d
You might want to consider using
async
instead of
launch
, then return the
feature
,
someFile
and
id
. Then collect the results in the
forEach
. To avoid the synchronization when using those collections.
Or use `Channel`s maybe but that might be unnecessary.
n
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 {}
d
I guess for the case of feature, you could use a
Channel
.
Then all the "locking" would be asynchronous.
n
is there any resources you could recommend to learn about Channels ?
That should do it.
n
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
d
That's because it is. ­čÖé
That's a more detailed resource if you need one.
n
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
d
Yeah, it more of a pipe than an actual collection.
d
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 -> }
n
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