Grouvie
04/24/2022, 5:16 PMGrouvie
04/24/2022, 5:16 PMjulian
04/24/2022, 6:27 PMjob.cancel()
.
Storing job references is simplified by Structured Concurrency.
Also, a Job
instance is returned by any call to launch
or async
coroutine builders.Grouvie
04/24/2022, 7:08 PMDavid Soller
04/25/2022, 3:23 AMGrouvie
04/25/2022, 11:55 AMCasey Brooks
04/25/2022, 2:34 PMJob
to the database, for exampleGrouvie
04/25/2022, 5:03 PMsuspend fun main() {
class Bot() {
val jobs = mutableListOf<Job>()
init {
val activeQuizzes = getActiveQuizzes()
for(username in activeQuizzes) {
start(username)
}
}
private fun getActiveQuizzes(): List<String> {
return listOf("User1", "User2")
}
fun start(username: String) {
val job = Job()
val scope = CoroutineScope(Dispatchers.Default + job)
scope.launch {
while (true) {
delay(500)
println("Delayed $username")
}
}
jobs.add(job)
}
fun stop(id: Int) {
println("Cancel job: $id")
jobs[id].cancel()
}
}
val bot = Bot()
bot.start("User3")
delay(5000)
bot.stop(2)
bot.jobs.joinAll()
}
Is it okay to use coroutines like this or should I do something completely different?