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
Next Next commit
added code
  • Loading branch information
pratikdas committed Jun 19, 2022
commit 80165c55ab93b3f37539f0b9ed33a602795bcd1d
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ fun main() = runBlocking{
val job:Job = launch {
val files = File ("/Users/10680240/Downloads/").listFiles()
var loop = 0
while (loop < files.size-1 && isActive) {
readFile(files.get(++loop))

while (loop < files.size-1 ) {
if(isActive) {
readFile(files.get(++loop))
}
}
}
delay(50)
delay(1500)
job.cancelAndJoin()

println("program run ends...: ${Thread.currentThread().name}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.pratik

import kotlinx.coroutines.*
import java.io.File

fun main() = runBlocking{
try {
val job1 = launch {
repeat(20){
println("processing job 1: ${Thread.currentThread().name}")
yield()
}
}

val job2 = launch {
repeat(20){
println("processing job 2: ${Thread.currentThread().name}")
yield()
}
}

job1.join()
job2.join()

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

}
}



33 changes: 10 additions & 23 deletions kotlin/coroutines/src/main/kotlin/io/pratik/CoroutineDispatch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,25 @@ fun main() = runBlocking {
println("Unconfined : running in thread ${Thread.currentThread().name}")
longTask()
}*/
/*launch(Dispatchers.Default) { // will get dispatched to DefaultDispatcher
println("Default : running in thread ${Thread.currentThread().name}")
longTask()
/*repeat(1000) {
val dispatcher = Dispatchers.Default.limitedParallelism(3)
launch(dispatcher) {
// launch(Dispatchers.Default) { // will get dispatched to DefaultDispatcher
println("Default : running in thread ${Thread.currentThread().name}")
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")
}

// Concurrently executes both sections
/*suspend fun longTask() {
launch { // context of the parent, main runBlocking coroutine
println("main runBlocking : I'm working in thread ${Thread.currentThread().name}")
longTask()
}
launch(Dispatchers.Unconfined) { // not confined -- will work with main thread
println("Unconfined : I'm working in thread ${Thread.currentThread().name}")
}
launch(Dispatchers.Default) { // will get dispatched to DefaultDispatcher
println("Default : I'm working in thread ${Thread.currentThread().name}")
}
launch(newSingleThreadContext("MyOwnThread")) { // will get its own new thread
println("newSingleThreadContext: I'm working in thread ${Thread.currentThread().name}")
}
println("completed task1 and task2")
}*/

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}")
}

3 changes: 2 additions & 1 deletion kotlin/coroutines/src/main/kotlin/io/pratik/MyApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ fun main() = runBlocking{
longRunningTask()
}

println("My program run ends...: ${Thread.currentThread().name}")
println("My program run ends...: " +
"${Thread.currentThread().name}")
}

suspend fun longRunningTask(){
Expand Down