Tushar
09/29/2024, 8:05 AMfun main(){
println("Am starting off")
runBlocking {
println("test is over" +coroutineTest())
}
println("Am finishing off")
}
fun coroutineTest(): Boolean{
CoroutineScope(Dispatchers.Default).launch {
for ( i in 1..20){
println("I am alive $i")
}
}
return true
}
Output:
Am starting off
test is overtrue
I am alive 1
I am alive 2
I am alive 3
I am alive 4
I am alive 5
I am alive 6
I am alive 7
I am alive 8
I am alive 9
I am alive 10
I am alive 11
Am finishing off
I am alive 12
I am alive 13
I am alive 14
I am alive 15
I am alive 16
I am alive 17
I am alive 18
I am alive 19
I am alive 20
tried out above code and now am confused why the coroutines still lives even after function finishes off
i thought the coroutineContext ensures coroutine doesn't outlives the scope
even adding ensureActive() doesn't make any difference but taking it out from runBlocking works as expected why?Pim
09/29/2024, 8:34 AMFrançois
09/29/2024, 8:34 AMPim
09/29/2024, 8:37 AMFrançois
09/29/2024, 8:40 AMsuspend fun coroutineTest(): Boolean =
withContext(Dispatchers.Default) {
for (i in 1..20) {
println("I am alive $i")
}
true
}
Tushar
09/29/2024, 8:44 AMensureActive()
just after println("I am alive $i")
but the behaviour remains same.Tushar
09/29/2024, 8:47 AMcoroutineTest()
out of runBlocking
works finejamshedalamqaderi
09/29/2024, 12:15 PMTushar
09/29/2024, 12:16 PMjamshedalamqaderi
09/29/2024, 12:19 PMCoroutineScope(Dispatchers.Default).launch
will detach from runBlocking
context and run in backgroundTushar
09/29/2024, 12:22 PMjamshedalamqaderi
09/29/2024, 12:23 PMrunBlocking
is made for execute suspending works in non-suspending functions.Tushar
09/29/2024, 12:25 PMjamshedalamqaderi
09/29/2024, 12:26 PMrunBlocking
?Tushar
09/29/2024, 12:26 PMjamshedalamqaderi
09/29/2024, 12:31 PMAm starting off
Test is over : true
Am finishing off
I am alive 1
I am alive 2
I am alive 3
I am alive 4
I am alive 5
I am alive 6
I am alive 7
I am alive 8
I am alive 9
I am alive 10
I am alive 11
I am alive 12
I am alive 13
I am alive 14
I am alive 15
I am alive 16
I am alive 17
I am alive 18
I am alive 19
I am alive 20
Tushar
09/29/2024, 12:32 PMjamshedalamqaderi
09/29/2024, 12:34 PMThread.sleep()
before return true
Tushar
09/29/2024, 12:38 PMjamshedalamqaderi
09/29/2024, 12:39 PMTushar
09/29/2024, 12:40 PMjamshedalamqaderi
09/29/2024, 12:40 PMTushar
09/29/2024, 12:42 PMjamshedalamqaderi
09/29/2024, 12:43 PMTushar
09/29/2024, 12:49 PMjamshedalamqaderi
09/29/2024, 1:00 PMsuspend
keyword in a function and finally if you want to execute it from main
function then use runBlocking
, So it will hold the main function running until suspending
function is finished. But CoroutineScope
need to be managed by youTushar
09/29/2024, 1:01 PMjamshedalamqaderi
09/29/2024, 1:15 PMCoroutineScope
that i have used in my projects. You could take another example to run long running task on cron jobs
// kotlin
fun main() {
println("Start of the main")
runBlocking {
val worker: Worker = PrimaryWorker()
if (!worker.isRunning) {
println("Worker is not running. Starting...")
worker.run()
}
delay(10.seconds)
if (worker.isRunning) {
println("Worker is still running. Stopping it")
worker.stop()
}
delay(1.seconds)
}
println("End of the main")
}
class PrimaryWorker : Worker {
private var job: Job? = null
override val isRunning: Boolean
get() = job != null && job?.isActive == true
override fun run() {
job = CoroutineScope(Dispatchers.IO).launch {
while (isActive) {
delay(1.seconds)
println("Running from primary worker")
}
}
job?.invokeOnCompletion {
println("Primary worker job is completed: ${it?.message}")
}
}
override fun stop() {
job?.cancel("Stopping manually")
}
}
interface Worker {
val isRunning: Boolean
fun run()
fun stop()
}
Tushar
09/29/2024, 1:20 PM