Skip to content

Instantly share code, notes, and snippets.

@oscarg798
Created January 14, 2020 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oscarg798/3dca6b80b2f2d200ae922e6e45958ff3 to your computer and use it in GitHub Desktop.
Save oscarg798/3dca6b80b2f2d200ae922e6e45958ff3 to your computer and use it in GitHub Desktop.
val m : (Int, Int) -> Unit = {x, y ->
print("::::$x $y")
}
CoroutineScope(Dispatchers.Default).launch {
print("Corutine 1 $this\n")
val mm = this
withContext(Dispatchers.IO) {
print("Corutine 2 $this\n")
print("Corutine 3 $this\n")
val x = async(Dispatchers.IO){
delay(100)
print("Corutine 4 $this ${Thread.currentThread()}\n")
mm.cancel()
4
}
val y = async(Dispatchers.Unconfined){
delay(200)
print("Corutine 5 $this ${Thread.currentThread()}\n")
3
}
m.invoke(x.await(), y.await())
}
}
CoroutineScope(Dispatchers.Default).launch {
print("Corutine 1 $this\n")
val mm = this
runBlocking {
print("Corutine 2 $this\n")
print("Corutine 3 $this\n")
val x = async(Dispatchers.IO){
delay(100)
print("Corutine 4 $this ${Thread.currentThread()}\n")
mm.cancel()
4
}
val y = async(Dispatchers.Unconfined){
delay(200)
print("Corutine 5 $this ${Thread.currentThread()}\n")
3
}
m.invoke(x.await(), y.await())
}
}
//This one will not block the parent coroutine
@oscarg798
Copy link
Author

private val jobs = kotlin.collections.mutableListOf<kotlinx.coroutines.Job>()

val x = CoroutineScope(Dispatchers.Default).launch {
    print("Corutine 1 $this\n")
    val parent1 = this


    runBlocking {
        async(Dispatchers.IO){
            delay(500)
            print("Corutine 2 $this ${Thread.currentThread()}\n")
            4
        }

        async(Dispatchers.IO){
            print("Corutine 3 $this ${Thread.currentThread()}\n")
            delay(200)
         
            print("Corutine 3 END")
        }
    }

    launch {
        print("Launching\n")
    }

    //m.invoke(x.await(), y.await())
    print("Corutine 1 END")

}
jobs.add(x)
print("AMIGOS\n")
jobs.forEach { it.cancel() }
print("BITCHES\n")```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment