code modified
This commit is contained in:
@@ -9,7 +9,6 @@ fun main() = runBlocking{
|
||||
val taskDeferred = async {
|
||||
generateUniqueID()
|
||||
}
|
||||
|
||||
val taskResult = taskDeferred.await()
|
||||
|
||||
println("program run ends...: ${taskResult} ${Thread.currentThread().name}")
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.pratik
|
||||
import kotlinx.coroutines.*
|
||||
import java.io.File
|
||||
|
||||
|
||||
fun main() = runBlocking{
|
||||
try {
|
||||
val job1 = launch {
|
||||
@@ -22,7 +23,7 @@ fun main() = runBlocking{
|
||||
job1.join()
|
||||
job2.join()
|
||||
|
||||
} catch (e: Exception) {
|
||||
} catch (e: CancellationException) {
|
||||
// clean up code
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
package io.pratik
|
||||
|
||||
import kotlinx.coroutines.*
|
||||
import kotlin.coroutines.ContinuationInterceptor
|
||||
|
||||
fun main() = runBlocking {
|
||||
/* launch {
|
||||
println(coroutineContext)
|
||||
launch {
|
||||
println(
|
||||
"launch default: running in thread ${Thread.currentThread().name} ${coroutineContext[ContinuationInterceptor]}")
|
||||
longTask()
|
||||
}*/
|
||||
}
|
||||
|
||||
/*launch(Dispatchers.Unconfined) { // not confined -- will work with main thread
|
||||
/* launch(Dispatchers.Unconfined) { // not confined -- will work with main thread
|
||||
println("Unconfined : running in thread ${Thread.currentThread().name}")
|
||||
longTask()
|
||||
}*/
|
||||
@@ -19,17 +23,17 @@ fun main() = runBlocking {
|
||||
longTask()
|
||||
}
|
||||
}*/
|
||||
launch(newSingleThreadContext("MyThread")) { // will get its own new thread
|
||||
/* launch(newSingleThreadContext("MyThread")) { // will get its own new thread
|
||||
println("newSingleThreadContext: running in thread ${Thread.currentThread().name}")
|
||||
longTask()
|
||||
}
|
||||
}*/
|
||||
println("completed tasks")
|
||||
}
|
||||
|
||||
|
||||
suspend fun longTask(){
|
||||
// println("executing longTask on...: ${Thread.currentThread().name}")
|
||||
println("executing longTask on...: ${Thread.currentThread().name}")
|
||||
delay(1000)
|
||||
// println("longTask ends on thread ...: ${Thread.currentThread().name}")
|
||||
println("longTask ends on thread ...: ${Thread.currentThread().name}")
|
||||
}
|
||||
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
package io.pratik
|
||||
|
||||
import kotlinx.coroutines.*
|
||||
import kotlin.coroutines.EmptyCoroutineContext
|
||||
|
||||
fun main() = runBlocking{
|
||||
println("My program runs...: ${Thread.currentThread().name}")
|
||||
|
||||
val job:Job = launch {
|
||||
val job:Job = launch (EmptyCoroutineContext, CoroutineStart.DEFAULT){
|
||||
longRunningTaskSuspended()
|
||||
}
|
||||
|
||||
job.join()
|
||||
/*runBlocking {
|
||||
delay(2000)
|
||||
}*/
|
||||
|
||||
println("My program run ends...: ${Thread.currentThread().name}")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package io.pratik
|
||||
|
||||
import kotlinx.coroutines.*
|
||||
import java.time.Instant
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
|
||||
fun main() = runBlocking{
|
||||
println("${Instant.now()}: My program runs...: ${Thread.currentThread().name}")
|
||||
val productId = findProduct()
|
||||
|
||||
launch (Dispatchers.Unconfined) {
|
||||
val price = fetchPrice(productId)
|
||||
}
|
||||
updateProduct()
|
||||
println("${Instant.now()}: My program run ends...: " +
|
||||
"${Thread.currentThread().name}")
|
||||
}
|
||||
|
||||
suspend fun fetchPrice(productId: String) : Double{
|
||||
println("${Instant.now()}: fetchPrice starts on...: ${Thread.currentThread().name} ")
|
||||
delay(2000)
|
||||
println("${Instant.now()}: fetchPrice ends on...: ${Thread.currentThread().name} ")
|
||||
return 234.5
|
||||
}
|
||||
|
||||
fun findProduct() : String{
|
||||
println("${Instant.now()}: findProduct on...: ${Thread.currentThread().name}")
|
||||
return "P12333"
|
||||
}
|
||||
|
||||
fun updateProduct() : String{
|
||||
println("${Instant.now()}: updateProduct on...: ${Thread.currentThread().name}")
|
||||
return "Product updated"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user