added code

This commit is contained in:
Pratik Das
2022-06-19 18:53:55 +05:30
parent e4f9bece6b
commit 80165c55ab
4 changed files with 50 additions and 27 deletions

View File

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

View File

@@ -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
}
}

View File

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

View File

@@ -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(){