File tree Expand file tree Collapse file tree 5 files changed +51
-13
lines changed
kotlin/coroutines/src/main/kotlin/io/pratik Expand file tree Collapse file tree 5 files changed +51
-13
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,6 @@ fun main() = runBlocking{
99 val taskDeferred = async {
1010 generateUniqueID()
1111 }
12-
1312 val taskResult = taskDeferred.await()
1413
1514 println (" program run ends...: ${taskResult} ${Thread .currentThread().name} " )
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package io.pratik
33import kotlinx.coroutines.*
44import java.io.File
55
6+
67fun main () = runBlocking{
78 try {
89 val job1 = launch {
@@ -22,7 +23,7 @@ fun main() = runBlocking{
2223 job1.join()
2324 job2.join()
2425
25- } catch (e: Exception ) {
26+ } catch (e: CancellationException ) {
2627 // clean up code
2728
2829 }
Original file line number Diff line number Diff line change 11package io.pratik
22
33import kotlinx.coroutines.*
4+ import kotlin.coroutines.ContinuationInterceptor
45
56fun main () = runBlocking {
6- /* launch {
7+ println (coroutineContext)
8+ launch {
9+ println (
10+ " launch default: running in thread ${Thread .currentThread().name} ${coroutineContext[ContinuationInterceptor ]} " )
711 longTask()
8- }*/
12+ }
913
10- /* launch(Dispatchers.Unconfined) { // not confined -- will work with main thread
14+ /* launch(Dispatchers.Unconfined) { // not confined -- will work with main thread
1115 println("Unconfined : running in thread ${Thread.currentThread().name}")
1216 longTask()
1317 }*/
@@ -19,17 +23,17 @@ fun main() = runBlocking {
1923 longTask()
2024 }
2125 }*/
22- launch(newSingleThreadContext(" MyThread" )) { // will get its own new thread
26+ /* launch(newSingleThreadContext("MyThread")) { // will get its own new thread
2327 println("newSingleThreadContext: running in thread ${Thread.currentThread().name}")
2428 longTask()
25- }
29+ }*/
2630 println (" completed tasks" )
2731}
2832
2933
3034suspend fun longTask (){
31- // println("executing longTask on...: ${Thread.currentThread().name}")
35+ println (" executing longTask on...: ${Thread .currentThread().name} " )
3236 delay(1000 )
33- // println("longTask ends on thread ...: ${Thread.currentThread().name}")
37+ println (" longTask ends on thread ...: ${Thread .currentThread().name} " )
3438}
3539
Original file line number Diff line number Diff line change 11package io.pratik
22
33import kotlinx.coroutines.*
4+ import kotlin.coroutines.EmptyCoroutineContext
45
56fun main () = runBlocking{
67 println (" My program runs...: ${Thread .currentThread().name} " )
78
8- val job: Job = launch {
9+ val job: Job = launch ( EmptyCoroutineContext , CoroutineStart . DEFAULT ) {
910 longRunningTaskSuspended()
1011 }
1112
1213 job.join()
13- /* runBlocking {
14- delay(2000)
15- }*/
1614
1715 println (" My program run ends...: ${Thread .currentThread().name} " )
1816}
Original file line number Diff line number Diff line change 1+ package io.pratik
2+
3+ import kotlinx.coroutines.*
4+ import java.time.Instant
5+ import kotlin.concurrent.thread
6+
7+
8+ fun main () = runBlocking{
9+ println (" ${Instant .now()} : My program runs...: ${Thread .currentThread().name} " )
10+ val productId = findProduct()
11+
12+ launch (Dispatchers .Unconfined ) {
13+ val price = fetchPrice(productId)
14+ }
15+ updateProduct()
16+ println (" ${Instant .now()} : My program run ends...: " +
17+ " ${Thread .currentThread().name} " )
18+ }
19+
20+ suspend fun fetchPrice (productId : String ) : Double {
21+ println (" ${Instant .now()} : fetchPrice starts on...: ${Thread .currentThread().name} " )
22+ delay(2000 )
23+ println (" ${Instant .now()} : fetchPrice ends on...: ${Thread .currentThread().name} " )
24+ return 234.5
25+ }
26+
27+ fun findProduct () : String {
28+ println (" ${Instant .now()} : findProduct on...: ${Thread .currentThread().name} " )
29+ return " P12333"
30+ }
31+
32+ fun updateProduct () : String {
33+ println (" ${Instant .now()} : updateProduct on...: ${Thread .currentThread().name} " )
34+ return " Product updated"
35+ }
36+
You can’t perform that action at this time.
0 commit comments