Skip to content

Commit 80165c5

Browse files
committed
added code
1 parent e4f9bec commit 80165c5

File tree

4 files changed

+50
-27
lines changed

4 files changed

+50
-27
lines changed

kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineCancel.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ fun main() = runBlocking{
99
val job:Job = launch {
1010
val files = File ("/Users/10680240/Downloads/").listFiles()
1111
var loop = 0
12-
while (loop < files.size-1 && isActive) {
13-
readFile(files.get(++loop))
12+
13+
while (loop < files.size-1 ) {
14+
if(isActive) {
15+
readFile(files.get(++loop))
16+
}
1417
}
1518
}
16-
delay(50)
19+
delay(1500)
1720
job.cancelAndJoin()
1821

1922
println("program run ends...: ${Thread.currentThread().name}")
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.pratik
2+
3+
import kotlinx.coroutines.*
4+
import java.io.File
5+
6+
fun main() = runBlocking{
7+
try {
8+
val job1 = launch {
9+
repeat(20){
10+
println("processing job 1: ${Thread.currentThread().name}")
11+
yield()
12+
}
13+
}
14+
15+
val job2 = launch {
16+
repeat(20){
17+
println("processing job 2: ${Thread.currentThread().name}")
18+
yield()
19+
}
20+
}
21+
22+
job1.join()
23+
job2.join()
24+
25+
} catch (e: Exception) {
26+
// clean up code
27+
28+
}
29+
}
30+
31+
32+

kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineDispatch.kt

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,25 @@ fun main() = runBlocking {
1111
println("Unconfined : running in thread ${Thread.currentThread().name}")
1212
longTask()
1313
}*/
14-
/*launch(Dispatchers.Default) { // will get dispatched to DefaultDispatcher
15-
println("Default : running in thread ${Thread.currentThread().name}")
16-
longTask()
14+
/*repeat(1000) {
15+
val dispatcher = Dispatchers.Default.limitedParallelism(3)
16+
launch(dispatcher) {
17+
// launch(Dispatchers.Default) { // will get dispatched to DefaultDispatcher
18+
println("Default : running in thread ${Thread.currentThread().name}")
19+
longTask()
20+
}
1721
}*/
18-
launch(newSingleThreadContext("MyThread")) { // will get its own new thread
22+
launch(newSingleThreadContext("MyThread")) { // will get its own new thread
1923
println("newSingleThreadContext: running in thread ${Thread.currentThread().name}")
2024
longTask()
2125
}
2226
println("completed tasks")
2327
}
2428

25-
// Concurrently executes both sections
26-
/*suspend fun longTask() {
27-
launch { // context of the parent, main runBlocking coroutine
28-
println("main runBlocking : I'm working in thread ${Thread.currentThread().name}")
29-
longTask()
30-
}
31-
launch(Dispatchers.Unconfined) { // not confined -- will work with main thread
32-
println("Unconfined : I'm working in thread ${Thread.currentThread().name}")
33-
}
34-
launch(Dispatchers.Default) { // will get dispatched to DefaultDispatcher
35-
println("Default : I'm working in thread ${Thread.currentThread().name}")
36-
}
37-
launch(newSingleThreadContext("MyOwnThread")) { // will get its own new thread
38-
println("newSingleThreadContext: I'm working in thread ${Thread.currentThread().name}")
39-
}
40-
println("completed task1 and task2")
41-
}*/
4229

4330
suspend fun longTask(){
44-
println("executing longTask on...: ${Thread.currentThread().name}")
31+
// println("executing longTask on...: ${Thread.currentThread().name}")
4532
delay(1000)
46-
println("longTask ends on thread ...: ${Thread.currentThread().name}")
33+
// println("longTask ends on thread ...: ${Thread.currentThread().name}")
4734
}
4835

kotlin/coroutines/src/main/kotlin/io/pratik/MyApp.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ fun main() = runBlocking{
1414
longRunningTask()
1515
}
1616

17-
println("My program run ends...: ${Thread.currentThread().name}")
17+
println("My program run ends...: " +
18+
"${Thread.currentThread().name}")
1819
}
1920

2021
suspend fun longRunningTask(){

0 commit comments

Comments
 (0)