File tree 5 files changed +51
-13
lines changed
kotlin/coroutines/src/main/kotlin/io/pratik
5 files changed +51
-13
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,6 @@ fun main() = runBlocking{
9
9
val taskDeferred = async {
10
10
generateUniqueID()
11
11
}
12
-
13
12
val taskResult = taskDeferred.await()
14
13
15
14
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
3
3
import kotlinx.coroutines.*
4
4
import java.io.File
5
5
6
+
6
7
fun main () = runBlocking{
7
8
try {
8
9
val job1 = launch {
@@ -22,7 +23,7 @@ fun main() = runBlocking{
22
23
job1.join()
23
24
job2.join()
24
25
25
- } catch (e: Exception ) {
26
+ } catch (e: CancellationException ) {
26
27
// clean up code
27
28
28
29
}
Original file line number Diff line number Diff line change 1
1
package io.pratik
2
2
3
3
import kotlinx.coroutines.*
4
+ import kotlin.coroutines.ContinuationInterceptor
4
5
5
6
fun main () = runBlocking {
6
- /* launch {
7
+ println (coroutineContext)
8
+ launch {
9
+ println (
10
+ " launch default: running in thread ${Thread .currentThread().name} ${coroutineContext[ContinuationInterceptor ]} " )
7
11
longTask()
8
- }*/
12
+ }
9
13
10
- /* launch(Dispatchers.Unconfined) { // not confined -- will work with main thread
14
+ /* launch(Dispatchers.Unconfined) { // not confined -- will work with main thread
11
15
println("Unconfined : running in thread ${Thread.currentThread().name}")
12
16
longTask()
13
17
}*/
@@ -19,17 +23,17 @@ fun main() = runBlocking {
19
23
longTask()
20
24
}
21
25
}*/
22
- launch(newSingleThreadContext(" MyThread" )) { // will get its own new thread
26
+ /* launch(newSingleThreadContext("MyThread")) { // will get its own new thread
23
27
println("newSingleThreadContext: running in thread ${Thread.currentThread().name}")
24
28
longTask()
25
- }
29
+ }*/
26
30
println (" completed tasks" )
27
31
}
28
32
29
33
30
34
suspend fun longTask (){
31
- // println("executing longTask on...: ${Thread.currentThread().name}")
35
+ println (" executing longTask on...: ${Thread .currentThread().name} " )
32
36
delay(1000 )
33
- // println("longTask ends on thread ...: ${Thread.currentThread().name}")
37
+ println (" longTask ends on thread ...: ${Thread .currentThread().name} " )
34
38
}
35
39
Original file line number Diff line number Diff line change 1
1
package io.pratik
2
2
3
3
import kotlinx.coroutines.*
4
+ import kotlin.coroutines.EmptyCoroutineContext
4
5
5
6
fun main () = runBlocking{
6
7
println (" My program runs...: ${Thread .currentThread().name} " )
7
8
8
- val job: Job = launch {
9
+ val job: Job = launch ( EmptyCoroutineContext , CoroutineStart . DEFAULT ) {
9
10
longRunningTaskSuspended()
10
11
}
11
12
12
13
job.join()
13
- /* runBlocking {
14
- delay(2000)
15
- }*/
16
14
17
15
println (" My program run ends...: ${Thread .currentThread().name} " )
18
16
}
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