Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
code modified
  • Loading branch information
pratikdas committed Jun 25, 2022
commit f531c21c6784c87aba3e22db27ea97aa8f02c002
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ fun main() = runBlocking{
val taskDeferred = async {
generateUniqueID()
}

val taskResult = taskDeferred.await()

println("program run ends...: ${taskResult} ${Thread.currentThread().name}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.pratik
import kotlinx.coroutines.*
import java.io.File


fun main() = runBlocking{
try {
val job1 = launch {
Expand All @@ -22,7 +23,7 @@ fun main() = runBlocking{
job1.join()
job2.join()

} catch (e: Exception) {
} catch (e: CancellationException) {
// clean up code

}
Expand Down
18 changes: 11 additions & 7 deletions kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineDispatch.kt
Original file line number Diff line number Diff line change
@@ -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()
}*/
Expand All @@ -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}")
}

Original file line number Diff line number Diff line change
@@ -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}")
}
Expand Down
36 changes: 36 additions & 0 deletions kotlin/coroutines/src/main/kotlin/io/pratik/SuspendingFunction.kt
Original file line number Diff line number Diff line change
@@ -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"
}