bitkid
10/03/2019, 1:51 PMbitkid
10/03/2019, 1:54 PMbitkid
10/03/2019, 1:55 PMobject Uploader {
@JvmStatic
fun main(args: Array<String>) {
val files = Channel<File>()
GlobalScope.launch {
uploader(files)
}
files.sendBlocking(File("a"))
}
}bitkid
10/03/2019, 1:55 PMbitkid
10/03/2019, 1:57 PMbitkid
10/03/2019, 2:11 PMreline
10/03/2019, 2:38 PMrunBlocking e.g.
fun main(args: Array<String>) = runBlocking { ... }bitkid
10/03/2019, 4:32 PMbitkid
10/03/2019, 4:33 PMreline
10/03/2019, 4:57 PMobject Uploader {
@JvmStatic
fun main(args: Array<String>) = runBlocking {
val files = Channel<File>()
GlobalScope.launch {
files.send(File("a"))
}
uploader(files)
}
}
uploader() to finishuploader() is an extension of CoroutineScope, correct?bitkid
10/03/2019, 5:33 PMbitkid
10/03/2019, 5:47 PMbitkid
10/03/2019, 5:49 PMbitkid
10/03/2019, 8:07 PMbitkid
10/03/2019, 8:30 PMreline
10/03/2019, 9:59 PMval files = (1..10).map { File("$it") }
val channel = Channel<File>()
launch {
files.forEach {
channel.send(it)
}
channel.close()
}
val time = measureTimeMillis {
coroutineScope {
uploader(channel)
}
}
println(time)reline
10/03/2019, 10:00 PMresponses.close() inside uploadWorker() after your for loopreline
10/03/2019, 10:22 PMcoroutineScope like this here is the right way to go about it, but the only alternative I can think of is making uploader() suspending
suspend fun uploader(files: ReceiveChannel<File>,
nrOfWorkers: Int = 4) = coroutineScope { ... }Brad Murray
10/04/2019, 1:30 AMbitkid
10/04/2019, 7:26 AMbitkid
10/04/2019, 7:27 AMbitkid
10/04/2019, 7:29 AMbitkid
10/04/2019, 7:35 AM