Skip to content

Commit

Permalink
Code Upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperancinha committed Jan 22, 2024
1 parent 32dd30c commit 803fe51
Showing 1 changed file with 2 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import kotlin.system.measureTimeMillis

class StructuredConcurrency {
companion object {

@JvmStatic
fun main(args: Array<String> = emptyArray()) {
launchTest(1, "should show blocking event loop") { testBLockingLaunchBlockEvenLoop() }
Expand All @@ -30,17 +29,15 @@ class StructuredConcurrency {
"should cancel the whole scope when failing a Dispatchers.IO context"
) { testCancelBlockingLaunchDispatchersIO() }
}

}

}

fun launchTest(id: Long, testName: String, f: () -> Unit) {
println("********************************************************************************************");
println("********************************************************************************************")
println("\nStarting test ($id): \"$testName\" at ${LocalDateTime.now()}")
println("The whole test ($id) took ${measureTimeMillis { f() }} milliseconds to complete!")
println("\nTest ($id) \"$testName finished at ${LocalDateTime.now()}")
println("********************************************************************************************");
println("********************************************************************************************")
}

class CancellationStructuredConcurrency {
Expand Down Expand Up @@ -73,11 +70,9 @@ class CancellationStructuredConcurrency {
measureTimeMillis {
runCatching {
coroutineScope {

launch {
logCoroutinesWithADelayOf(1, 1000)
}

launch {
launch {
delay(500)
Expand All @@ -87,7 +82,6 @@ class CancellationStructuredConcurrency {
}
println("This coroutine scope is launched here! ${this.coroutineContext}")
}

}
}.let { println("As expected, this run took only $it milliseconds to complete.") }
println("When the coroutineScope is finished, I can finally finish the program")
Expand All @@ -101,15 +95,13 @@ class CancellationStructuredConcurrency {
launch {
logCoroutinesWithADelayOf(1, 1000)
}

launch {
logCoroutinesWithADelayOf(2, 1000)
}
launch {
delay(500)
throw RuntimeException("Nothing runs now!")
}

println("--------")
println(this.toString())
println(this.coroutineContext.job.key.toString())
Expand All @@ -129,7 +121,6 @@ class CancellationStructuredConcurrency {
}

class StandardStructuredConcurrency {

companion object {
fun testBLockingLaunchBlockEvenLoop() = runBlocking {
measureTimeMillis {
Expand Down

0 comments on commit 803fe51

Please sign in to comment.