Tolriq
06/05/2019, 9:10 AMclass XXX : CoroutineScope {
override val coroutineContext = <http://Dispatchers.IO|Dispatchers.IO> + SupervisorJob()
internal val tasks = Channel<Task>(UNLIMITED)
fun startWorkers() {
repeat(150) {
launch {
for (task in tasks) executeTask(task)
}
}
}
}
Zach Klippenstein (he/him) [MOD]
06/05/2019, 5:13 PMDefault
dispatcher (smaller, fixed-size thread pool, so you’re not gonna create a bunch of threads), then inside each of the workers you wait to switch to the IO
dispatcher until you have the first task?
launch(Unconfined) {
var initialTask = tasks.receive()
withContext(IO) {
executeTask(initialTask)
for (task in tasks) executeTask(task)
}
}
Unconfined
to launch them, since the only work they’d be doing initially would be registering to receive from the channel.Tolriq
06/05/2019, 6:31 PMZach Klippenstein (he/him) [MOD]
06/05/2019, 7:17 PMTolriq
06/05/2019, 7:39 PMZach Klippenstein (he/him) [MOD]
06/05/2019, 10:17 PMfun startWorkers() {
repeat(150) {
startWorker()
}
}
private fun startWorker() {
launch(Unconfined) {
var initialTask = tasks.receive()
withContext(IO) {
executeTask(initialTask)
for (task in tasks) executeTask(task)
}
}
}
Tolriq
06/06/2019, 6:27 AMZach Klippenstein (he/him) [MOD]
06/06/2019, 3:02 PMDefault
too for that initial dispatcher.Tolriq
06/06/2019, 6:42 PMrepeat(maxConcurrentTasks) {
launch(Dispatchers.Unconfined) {
Log.e("AAAA", "Launch ${Thread.currentThread().name}" )
val initialTask = try {
tasks.receive()
} catch (e: Exception) {
return@launch
}
withContext(this@OkHttpWorkerPool.coroutineContext) {
Log.e("AAAA", "Run ${Thread.currentThread().name}" )
executeTask(initialTask)
for (task in tasks) executeTask(task)
}
}
}
Will only log 4 Launch main thread