global dispacher : this will run on a separate thread without blocking the
summoner thread :
- Code:
-
fun main(){
exampleLaunchGlobal()
}
fun exampleLaunchGlobalWaiting() = runBlocking {
println("one - from thread ${Thread.currentThread().name}")
val job = GlobalScope.launch {
printlnDelayed("two - from thread ${Thread.currentThread().name}")
}
println("three - from thread ${Thread.currentThread().name}")
job.join()
}
suspend fun printlnDelayed(message: String) {
//this is a coroutin
// Complex calculation
delay(1000)
println(message)
}
################################
coroutin on a launch scope :
fun exampleLaunchCoroutineScope() = runBlocking {
println("one - from thread ${Thread.currentThread().name}")
launch {
printlnDelayed("two - from thread ${Thread.currentThread().name}")
}
println("three - from thread ${Thread.currentThread().name}")
}
those will all run on the same thread
to change the thread of the dispacher :
- Code:
-
fun exampleLaunchCoroutineScope() = runBlocking {
println("one - from thread ${Thread.currentThread().name}")
launch(Dispatchers.Default) {
printlnDelayed("two - from thread ${Thread.currentThread().name}")
}
println("three - from thread ${Thread.currentThread().name}")
}
also not to access the views like text views on android, this can oly be done via the
main thread or Dispacher.main
using your own custom dispacher :
- Code:
-
fun exampleLaunchCoroutineScope() = runBlocking {
println("one - from thread ${Thread.currentThread().name}")
val customDispatcher = Executors.newFixedThreadPool(2).asCoroutineDispatcher()
launch(customDispatcher) {
printlnDelayed("two - from thread ${Thread.currentThread().name}")
}
println("three - from thread ${Thread.currentThread().name}")
}
####################################
async with value get set:
- Code:
-
suspend fun calculateHardThings(startNum: Int): Int {
delay(1000)
return startNum * 10
}
fun exampleAsyncAwait() = runBlocking {
val startTime = System.currentTimeMillis()
val deferred1 = async { calculateHardThings(10) }
val deferred2 = async { calculateHardThings(20) }
val deferred3 = async { calculateHardThings(30) }
val sum = deferred1.await() + deferred2.await() + deferred3.await()
println("async/await result = $sum")
val endTime = System.currentTimeMillis()
println("Time taken: ${endTime - startTime}")
}
you can also use await like this :
val deferred1 = async { calculateHardThings(10) }.await()
#############################################################
kotlin coroutins async timer
- Code:
-
suspend fun calculateHardThings(startNum: String){
delay(1000)
println(startNum)
}
fun exampleAsyncAwait() = runBlocking {
val startTime = System.currentTimeMillis()
var c1:Int=1;
while (c1<10){
c1++;
val deferred1 = async { calculateHardThings("tick") }
val deferred2 = async { calculateHardThings("tok") }.await()
}
println("end")
}
###############################################################
if you want to run things async but not block the thread and with context:
which is the same as calling await after each async.
this is best used for heavy calculations so as to not burden the main or UI thread
- Code:
-
suspend fun calculateHardThings(startNum: Int): Int {
delay(1000)
return startNum * 10
}
fun exampleWithContext() = runBlocking {
val startTime = System.currentTimeMillis()
val result1 = withContext(Dispatchers.Default) { calculateHardThings(10) }
val result2 = withContext(Dispatchers.Default) { calculateHardThings(20) }
val result3 = withContext(Dispatchers.Default) { calculateHardThings(30) }
val sum = result1 + result2 + result3
println("async/await result = $sum")
val endTime = System.currentTimeMillis()
println("Time taken: ${endTime - startTime}")
}
###################################
timer on BG thread :
- Code:
-
fun main(){
exampleAsyncAwait()
println("main thread end")
}
suspend fun calculateHardThings(startNum: String){
delay(1000)
println(startNum)
}
fun exampleAsyncAwait() = runBlocking {
val startTime = System.currentTimeMillis()
var c1:Int=1;
launch { while (c1<10){
c1++;
val deferred1 = async { calculateHardThings("tick") }
val deferred2 = async { calculateHardThings("tok") }.await()
} }
println("end")//main code thread, does not wait for the timer which is in the BackGround
}