From f531c21c6784c87aba3e22db27ea97aa8f02c002 Mon Sep 17 00:00:00 2001 From: Pratik Das Date: Sat, 25 Jun 2022 23:36:15 +0530 Subject: [PATCH] code modified --- .../main/kotlin/io/pratik/CoroutineAsync.kt | 1 - .../kotlin/io/pratik/CoroutineCancelYield.kt | 3 +- .../kotlin/io/pratik/CoroutineDispatch.kt | 18 ++++++---- .../kotlin/io/pratik/CoroutineWithLaunch.kt | 6 ++-- .../kotlin/io/pratik/SuspendingFunction.kt | 36 +++++++++++++++++++ 5 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 kotlin/coroutines/src/main/kotlin/io/pratik/SuspendingFunction.kt diff --git a/kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineAsync.kt b/kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineAsync.kt index 78b72ef..84e5f30 100644 --- a/kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineAsync.kt +++ b/kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineAsync.kt @@ -9,7 +9,6 @@ fun main() = runBlocking{ val taskDeferred = async { generateUniqueID() } - val taskResult = taskDeferred.await() println("program run ends...: ${taskResult} ${Thread.currentThread().name}") diff --git a/kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineCancelYield.kt b/kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineCancelYield.kt index 5e1f369..3c00fe4 100644 --- a/kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineCancelYield.kt +++ b/kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineCancelYield.kt @@ -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 } diff --git a/kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineDispatch.kt b/kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineDispatch.kt index 71e9ea6..b74b37a 100644 --- a/kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineDispatch.kt +++ b/kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineDispatch.kt @@ -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}") } diff --git a/kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineWithLaunch.kt b/kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineWithLaunch.kt index 317bf24..94b520c 100644 --- a/kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineWithLaunch.kt +++ b/kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineWithLaunch.kt @@ -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}") } diff --git a/kotlin/coroutines/src/main/kotlin/io/pratik/SuspendingFunction.kt b/kotlin/coroutines/src/main/kotlin/io/pratik/SuspendingFunction.kt new file mode 100644 index 0000000..11a0361 --- /dev/null +++ b/kotlin/coroutines/src/main/kotlin/io/pratik/SuspendingFunction.kt @@ -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" +} +