added code
This commit is contained in:
@@ -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}")
|
||||
|
||||
@@ -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
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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}")
|
||||
}
|
||||
|
||||
|
||||
@@ -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(){
|
||||
|
||||
Reference in New Issue
Block a user